Cómo ejecutar la fase de compilación maven con dependencias de test-jar

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 instalar phase can be executed successfully. But trying to run the compilar or test phase fails, because the test jar file dependency cannot be resolved.

En cuanto al tarro de prueba goal, it seems to be configured to run by default during the paquete phase, which I think is the cause of the problem.

Then, I tried to force this goal to run during the compilar 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 compilar 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 compilar 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 compilar or test ¿fase?

preguntado el 24 de agosto de 12 a las 08:08

Had a similar problem with some plugins. Interested. -

Had a similiar problem yesterday, then I realized that I had the maven.skip.tests option set to true. Could it be somthing similar in your case? -

your declaration <type>test-jar</type> seems a bit strange, could you show us the header of the pom of this related module ? -

Why don't you wanna run mvn install? Eche un vistazo a la Referencia del ciclo de vida de Maven. Deberías poder usar el mvn package mando. -

@Farid It is because he has followed this guide from Maven: maven.apache.org/guides/mini/guide-attached-tests.html -

3 Respuestas

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>

Respondido el 04 de Septiembre de 13 a las 03:09

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.

Espero que ayude.

Respondido 24 ago 12, 10:08

I strongly believe that tests should only be part of one module. You should not depend on tests in other modules. It is very hard to predict what happens if you update the tests to behave differently.

If you need to share common test data or common test classes then it is better to create a separate module with that shared test source. And then let all tests have a dependency on the shared test-jar with scope test.

+-- MyProject
+-- common-test-util
|   +-- src
|   |    +-- main
|   |        +-- java
|   +-- pom.xml
+-- moduleA
|   +-- src
|   |    +-- main
|   |    |   +-- java
|   |    +-- test
|   |        +-- java
|   +-- pom.xml
+-- moduleB
|   +-- src
|   |    +-- main
|   |    |   +-- java
|   |    +-- test
|   |        +-- java
|   +-- pom.xml
+-- pom.xml

Make sure that you only depend on common-test-util con <scope>test</scope> and then you will be able to call

mvn test

on the top level and all tests will run.

Respondido 24 ago 12, 09:08

I respectfully dissagree. Mocks, Test Doubles, Stubs, Drivers and other test framework code is often usefully shared between modules but should not be included in runtime code. Building a common test-jar along with your regular common code jar works very well. Although of course I will agree that tests specific to the internals of the common jar shouldn't be used in other modules. You could separate these two categories of code but it is often not worth the effort. If you have a large test framework then make it a separate project of course. - AutomatizadoMike

@AutomatedMike That's more or less what I am saying. Put all that stuff in a jar and use in the different tests in other modules. But don't try to stuff it in src/test/java. Just put your actual tests there. - estudiante de primer año

@maba - moduleB is integration-tests, I have a dto in moduleA, I need to place a Fixture somewhere that I can use both in moduleA and moduleB. What's your suggestion in this case? - juanlemon

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.