Maven:它不适用于Java代码吗?

时间:2014-01-03 13:57:34

标签: maven pom.xml

我是maven的新手(只玩了2天)。 我在使用jacoco插件和surefire / failsafe设置maven时遇到了一些问题。 尽管它读取并复制源文件但它不会编译它们! 我设置了新目录,因为我没有遵循标准的maven目录布局,但它似乎无法正常工作。问题是我不能改变我的项目布局,因为它是公司的。 所以,我试图手动解决它。

pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.codehaus.sonar</groupId>
    <artifactId>example-java-maven</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>Code Coverage - Maven and JaCoCo running tests</name>

    <properties>
    <!--  Tell SonarQube to execute unit tests during analysis-->
    <sonar.dynamicAnalysis>true</sonar.dynamicAnalysis>
     <!-- Tell SonarQube to use Jacoco as the code coverage engine to generate the reports -->
    <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
    <!-- Display logs to see where the analyzer spends time -->
    <sonar.showProfiling>true</sonar.showProfiling>
    <sonar.branch>14.1</sonar.branch>
    <sonar.language>tcl</sonar.language>

    <project.exclude.list>target/**</project.exclude.list>
    <test.failOnError>true</test.failOnError>

    <basedir>.</basedir>

    <!-- Used to locate the profile specific configuration file. -->
    <build.profile.id>dev</build.profile.id>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

    <!-- Only unit tests are run by default. -->
    <skip.integration.tests>true</skip.integration.tests>
    <skip.unit.tests>false</skip.unit.tests>

    <!-- Define where the jacoco reports are output -->
    <jacoco.it.execution.data.file>${project.build.directory}/coverage-reports/jacoco-it.exec</jacoco.it.execution.data.file>
        <jacoco.ut.execution.data.file>${project.build.directory}/coverage-reports/jacoco-ut.exec</jacoco.ut.execution.data.file>

    <jacoco.it.execution.destfile>${project.build.directory}/coverage-reports/jacoco-it.exec</jacoco.it.execution.destfile>
        <jacoco.ut.execution.destfile>${project.build.directory}/coverage-reports/jacoco-ut.exec</jacoco.ut.execution.destfile>
    </properties>

    <!-- Creating profiles for Development and Integration Testing
    'dev' profile (default): used during development, only unit tests are run.
    'integration-test' profile: used to run integration tests. -->
    <profiles>
    <profile>
        <id>dev</id>
        <!-- Dev profile is active by default -->
        <activation>
        <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
        <!--Used to locate the profile specific configuration file-->
        <build.profile.id>dev</build.profile.id>
        <!-- Only unit tests are run by default. -->
        <skip.integration.tests>true</skip.integration.tests>
        <skip.unit.tests>false</skip.unit.tests>
        </properties>
        <build>
        <filters>
            <!--Specifies path to the properties file, which contains profile specific configuration-->
            <filter>profiles/${build.profile.id}/config.properties</filter>
        </filters>
        </build>
    </profile>
    <profile>
        <id>integration-test</id>
        <properties>
        <!--Used to locate the profile specific configuration file-->
        <build.profile.id>integration-test</build.profile.id>
        <!-- Only integration tests are run. -->
        <skip.integration.tests>false</skip.integration.tests>
        <skip.unit.tests>true</skip.unit.tests>
        </properties>
        <build>
        <filters>
            <!-- Specifies path to the properties file, which contains profile specific configuration. -->
            <filter>profiles/${build.profile.id}/config.properties</filter>
        </filters>
        </build>
    </profile>
    <profile>
            <id>all-tests</id>
            <properties>
                <build.profile.id>all-tests</build.profile.id>
                <!-- All tests are run. -->
                <skip.integration.tests>false</skip.integration.tests>
                <skip.unit.tests>false</skip.unit.tests>
            </properties>
        </profile>
    </profiles>

    <build>
    <finalName>maven-integration-testing</finalName>
    <sourceDirectory>.</sourceDirectory>
    <testSourceDirectory>test/tcl</testSourceDirectory>
    <filters>
            <filter>profiles/${build.profile.id}/config.properties</filter>
        </filters>
    <resources>
            <resource>
                <filtering>true</filtering>
                <directory>.</directory>
        <includes>
           <include>*.tcl</include>
        </includes>
            </resource>
        </resources>
    <testResources>
            <testResource>
                <filtering>true</filtering>
                <directory>test/tcl</directory>
            </testResource>
        </testResources>

    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>sonar-maven-plugin</artifactId>
            <version>2.2</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
            <compilerVersion>1.5</compilerVersion>
            <source>1.5</source>
            <target>1.5</target>
            <showWarnings>true</showWarnings>
            <encoding>${project.build.sourceEncoding}</encoding>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-site-plugin</artifactId>
            <version>3.3</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-scm-plugin</artifactId>
            <version>1.8.1</version>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4</version>
        </plugin>
        <plugin>
            <artifactId>maven-release-plugin</artifactId>
            <version>2.4.2</version>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.8</version>
            <executions>
            <!-- States that the plugin's add-test-source goal is
                executed at generate-test-sources phase. -->
            <execution>
                <id>add-integration-test-sources</id>
                <phase>generate-test-sources</phase>
                <goals>
                <goal>add-test-source</goal>
                </goals>
                <configuration>
                <!-- Configures the source directory of integration tests -->
                <sources>
                    <source>test/tcl</source>
                </sources>
                </configuration>
            </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.8</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.3</version>
            <configuration>
            <encoding>${project.build.sourceEncoding}</encoding>
            </configuration>
        </plugin>
        <!-- Use the Surefire Maven plugin to run our unit tests -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.16</version>
            <dependencies>
              <dependency>
                <groupId>org.apache.maven.surefire</groupId>
                <artifactId>surefire-junit47</artifactId>
                <version>2.16</version>
              </dependency>
            </dependencies>
            <configuration>
                <!-- Sets the VM argument line used when unit tests are run.
                we want to create a code coverage report for our unit tests,
                we have to ensure that the JaCoCo agent is running
                when our unit tests are run -->
                <argLine>${surefireArgLine}</argLine>
                <!-- Skips unit tests if the value of skip.unit.tests property is true -->
                <skipTests>${skip.unit.tests}</skipTests>
                <includes>
                <include>test/tcl/*.tcl</include>
                </includes>
                <!-- Excludes integration tests when unit tests are run. -->
                <excludes>
                <exclude>**/IT*.java</exclude>
                </excludes>
                <properties>
                <property>
                  <name>listener</name>
                  <value>org.sonar.java.jacoco.JUnitListener</value>
                </property>
                </properties>
            </configuration>
        </plugin>
        <!-- Failsafe Maven plugin is used to execute our integration tests -->
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-failsafe-plugin</artifactId>
          <version>2.16</version>
          <executions>
            <!-- States that both integration-test and verify goals
                of the Failsafe Maven plugin are executed. -->
            <execution>
                <id>integration-tests</id>
                <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
                </goals>
                <configuration>
                <!-- we want to create a code coverage report for our integration tests,
                 we have to ensure that the JaCoCo agent is running
                 when our integration tests are run -->
                 <argLine>${failsafeArgLine}</argLine>

                <!-- Skips integration tests if the value of skip.integration.tests property is true -->
                <skipTests>${skip.integration.tests}</skipTests>
                </configuration>
            </execution>
        </executions>
        </plugin>
        <!-- JaCoCo Maven Plugin
        1) provides us an access to the JaCoCo runtime agent which records
         execution coverage data.
        2) creates code coverage reports from the execution data recorded
        by the JaCoCo runtime agent.
        -->
        <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.6.4.201312101107</version>
        <executions>
            <!-- Make the agent run before the tests are run -->
            <execution>
            <id>jacoco-initialize</id>
            <goals>
              <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <!-- Sets the path to the file which contains the execution data. -->
                <destFile>${jacoco.ut.execution.data.file}</destFile>

                <!-- Sets the name of the property containing the settings for JaCoCo runtime agent. -->
                <propertyName>surefireArgLine</propertyName>
            </configuration>
            </execution>
            <!-- Make sure that the jacoco report task is run when package is executed -->
            <execution>
            <id>jacoco-site</id>
            <phase>package</phase>
            <goals>
              <goal>report</goal>
            </goals>
            </execution>
            <!-- Prepares the property pointing to the JaCoCo runtime agent which
            is passed as VM argument when Maven the Surefire plugin is executed.-->
            <execution>
            <id>pre-unit-test</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <!-- Sets the path to the file which contains the execution data. -->
                <destFile>${jacoco.ut.execution.data.file}</destFile>

                <!-- Sets the name of the property containing the settings for JaCoCo runtime agent. -->
                <propertyName>surefireArgLine</propertyName>
            </configuration>
            </execution>
            <!--Ensures that the code coverage report for unit tests is
            created after unit tests have been run.  -->
            <execution>
            <id>post-unit-test</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
            </goals>
            <configuration>
                <!-- Sets the path to the file which contains the execution data. -->
                <dataFile>${jacoco.ut.execution.data.file}</dataFile>

                <!-- Sets the output directory for the code coverage report. -->
                <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
            </configuration>
            </execution>
            <!-- Prepares the property pointing to the JaCoCo runtime agent which
               is passed as VM argument when Maven the Failsafe plugin is executed. -->
            <execution>
            <id>pre-integration-test</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <!-- Sets the path to the file which contains the execution data. -->
                <destFile>${jacoco.it.execution.data.file}</destFile>

                <!-- Sets the name of the property containing the settings for JaCoCo runtime agent.-->
                <propertyName>failsafeArgLine</propertyName>
            </configuration>
            </execution>
            <!-- Ensures that the code coverage report for integration tests after integration tests have been run.-->
            <execution>
            <id>post-integration-test</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>report</goal>
            </goals>
            <configuration>
                <!-- Sets the path to the file which contains the execution data. -->
                <dataFile>${jacoco.it.execution.data.file}</dataFile>

                <!-- Sets the output directory for the code coverage report. -->
                <outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
            </configuration>
            </execution>
        </executions>
        </plugin>
    </plugins>
    </build>

    <dependencies>
     <!-- TESTING DEPENDENCIES -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>
    </dependencies>


</project>

**output**:
[INFO] ------------------------------------------------------------------------
[INFO] Building Code Coverage - Maven and JaCoCo running tests 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- jacoco-maven-plugin:0.6.4.201312101107:prepare-agent (jacoco-initialize) @ example-java-maven ---
[INFO] surefireArgLine set to -javaagent:.m2/repository/org/jacoco/org.jacoco.agent/0.6.4.201312101107/org.jacoco.agent-0.6.4.201312101107-runtime.jar=destfile=/util/target/coverage-reports/jacoco-ut.exec
[INFO] 
[INFO] --- jacoco-maven-plugin:0.6.4.201312101107:prepare-agent (pre-unit-test) @ example-java-maven ---
[INFO] surefireArgLine set to -javaagent:.m2/repository/org/jacoco/org.jacoco.agent/0.6.4.201312101107/org.jacoco.agent-0.6.4.201312101107-runtime.jar=destfile=/util/target/coverage-reports/jacoco-ut.exec
[INFO] 
[INFO] --- maven-resources-plugin:2.3:resources (default-resources) @ example-java-maven ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 41 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ example-java-maven ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- build-helper-maven-plugin:1.8:add-test-source (add-integration-test-sources) @ example-java-maven ---
[INFO] Test Source directory:util/test/tcl added.
[INFO] 
[INFO] --- maven-resources-plugin:2.3:testResources (default-testResources) @ example-java-maven ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 5 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ example-java-maven ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.16:test (default-test) @ example-java-maven ---
[INFO] 
[INFO] --- jacoco-maven-plugin:0.6.4.201312101107:report (post-unit-test) @ example-java-maven ---
[INFO] Skipping JaCoCo execution due to missing execution data file
[INFO] 
[INFO] --- maven-jar-plugin:2.2:jar (default-jar) @ example-java-maven ---
[INFO] Building jar:util/target/maven-integration-testing.jar
[INFO] 
[INFO] --- jacoco-maven-plugin:0.6.4.201312101107:report (jacoco-site) @ example-java-maven ---
[INFO] Skipping JaCoCo execution due to missing execution data file
[INFO] 
[INFO] --- jacoco-maven-plugin:0.6.4.201312101107:prepare-agent (pre-integration-test) @ example-java-maven ---
[INFO] failsafeArgLine set to -javaagent:.m2/repository/org/jacoco/org.jacoco.agent/0.6.4.201312101107/org.jacoco.agent-0.6.4.201312101107-runtime.jar=destfile=util/target/coverage-reports/jacoco-it.exec
[INFO] 
[INFO] --- maven-failsafe-plugin:2.16:integration-test (integration-tests) @ example-java-maven ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- jacoco-maven-plugin:0.6.4.201312101107:report (post-integration-test) @ example-java-maven ---
[INFO] Skipping JaCoCo execution due to missing execution data file
[INFO] 
[INFO] --- maven-failsafe-plugin:2.16:verify (integration-tests) @ example-java-maven ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-install-plugin:2.3:install (default-install) @ example-java-maven ---
[INFO] Installing util/target/maven-integration-testing.jar to .m2/repository/org/codehaus/sonar/example-java-maven/1.0-SNAPSHOT/example-java-maven-1.0-SNAPSHOT.jar
[INFO] Installing util/pom.xml to .m2/repository/org/codehaus/sonar/example-java-maven/1.0-SNAPSHOT/example-java-maven-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.054s
[INFO] Finished at: Fri Jan 03 13:35:38 GMT 2014
[INFO] Final Memory: 12M/132M
[INFO] ------------------------------------------------------------------------

&GT;

修改 另外,我想知道Maven是否适用于不使用Java代码的项目。 有没有办法让它支持脚本语言?并运行测试?

0 个答案:

没有答案