Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run maven compile phase with test-jar dependencies

The project I'm working on is made of multiple modules, being built with maven. The test code in some modules has dependencies on test code from other modules. These dependencies are declared as below.

In the dependency module:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.2</version>
    <executions>
        <execution>
            <goals>
                <goal>test-jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>

In the module which has the dependency on the previous module:

<dependency>
    <groupId>${project.groupId}</groupId>
    <artifactId>themodulename</artifactId>
    <version>${project.version}</version>
    <type>test-jar</type>
    <scope>test</scope>
</dependency>

Using this configuration, the maven install phase can be executed successfully. But trying to run the compile or test phase fails, because the test jar file dependency cannot be resolved.

Looking at the test-jar goal, it seems to be configured to run by default during the package phase, which I think is the cause of the problem.

Then, I tried to force this goal to run during the compile phase, by modifying the first config into:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.2</version>
    <executions>
        <execution>
            <phase>compile</phase>
            <goals>
                <goal>test-jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Looking at the debug logs, I can see that the goal is now executed during the compile phase, but also this:

[WARNING] JAR will be empty - no content was marked for inclusion!

I tried to configure the includes to **/* and confirmed that the default testClassesDirectory was set to the right one, but I still get the same warning.

I could see that the test-classes folder didn't exist after running the compile phase, which seems normal, but even though it exists after running the test phase, and it contains files, I still get the "JAR will be empty" warning.

Does anyone have any idea on fixing this configuration so that I can run successfully the compile or test phase?

like image 862
killy971 Avatar asked Aug 24 '12 07:08

killy971


People also ask

Does Maven test compile?

Maven will compile the tests without running them.

How do I run a dependency in Maven?

Right-click the utility project, and select Maven>Add Dependency. Type a dependency name in the Enter groupID… field (e.g., commons-logging) to search for a dependency. Select the dependency, and click OK.


2 Answers

I think that these plugin configuration works for you.

Just overwrite the skip to false in resources preparation and compiling.

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <executions>
      <execution>
        <id>default-testResources</id>
        <configuration>
          <skip>false</skip>
        </configuration>
        <phase>process-test-resources</phase>
        <goals>
          <goal>testResources</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <executions>
      <execution>
        <id>default-testCompile</id>
        <configuration>
          <skip>false</skip>
        </configuration>
        <phase>test-compile</phase>
        <goals>
          <goal>testCompile</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <executions>
      <execution>
        <configuration>
          <skip>false</skip>
        </configuration>
        <goals>
          <goal>test-jar</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
like image 168
Rain Young Avatar answered Sep 28 '22 18:09

Rain Young


Your issue is down to dependency resolution in multi-module builds. It is not specifically related to test code.

I have exactly this setup. A common module contains runtime code along with shared test code (test doubles & mocks etc). The test code is used by tests in other modules. It works very well for us.

"mvn compile" only compiles runtime code.

Running an "mvn test-compile", "mvn test" or "mvn package" at the parent level (reactor build) everything works perfectly. The reactor can sort it all out.

If you run a build at the module level then all the depedencies of that module have to be in a repo. This means that you must have prevously run an "mvn install" for each of the dependent modules. This rule applies equally to both regular dependencies and test ones.

If you were hoping that it would follow the parent link up to the parent and down to the other modules I have to disappoint you. The parent link is only used to inherit common settings in the pom and not for dependency resolution.

Personally I almost always do a full reactor build from the parent. I only do an individual module build if I have previously run an mvn install at the parent level and I know the other modules haven't changed.

Hope that helps.

like image 25
AutomatedMike Avatar answered Sep 28 '22 17:09

AutomatedMike