Maven test dependency in multi module project

后端 未结 3 657
难免孤独
难免孤独 2020-11-27 10:26

I use maven to build a multi module project. My module 2 depends on Module 1 src at compile scope and module 1 tests in test scope.

Module 2 -

   <         


        
相关标签:
3条回答
  • 2020-11-27 10:52

    I have a doubt about what you are trying to do but but I'll assume you want to reuse the tests that you have created for a project (module1) in another. As explained in the note at the bottom of the Guide to using attached tests:

    Note that previous editions of this guide suggested to use <classifier>tests</classifier> instead of <type>test-jar</type>. While this currently works for some cases, it does not properly work during a reactor build of the test JAR module and any consumer if a lifecycle phase prior to install is invoked. In such a scenario, Maven will not resolve the test JAR from the output of the reactor build but from the local/remote repository. Apparently, the JAR from the repositories could be outdated or completely missing, causing a build failure (cf. MNG-2045).

    So, first, to package up compiled tests in a JAR and deploy them for general reuse, configure the maven-jar-plugin as follows:

    <project>
      <build>
        <plugins>
         <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>
        </plugins>
      </build>
    </project>
    

    Then, install/deploy the test JAR artifact as usual (using mvn install or mvn deploy).

    Finally, to use the test JAR, you should specify a dependency with a specified type of test-jar:

    <project>
      ...
      <dependencies>
        <dependency>
          <groupId>com.myco.app</groupId>
          <artifactId>foo</artifactId>
          <version>1.0-SNAPSHOT</version>
          <type>test-jar</type>
          <scope>test</scope>
        </dependency>
      </dependencies>
      ...
    </project>
    
    0 讨论(0)
  • 2020-11-27 11:08

    Regarding to my comment to Pascals question i think i have found a stuitable answer :

    <plugins>
        <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.2</version>
            <executions>
                <execution>
                <goals>
                    <goal>test-jar</goal>
                </goals>
                <phase>test-compile</phase>
            </execution>
            </executions>
            <configuration>
                <outputDirectory>${basedir}\target</outputDirectory>
            </configuration>
        </plugin>
    </plugins>
    

    The main difference here as you see here is the <phase> tag.

    I will create the test-jar and it will be available in the compile phase of the tests and not only after the package phase.

    Works for me.

    0 讨论(0)
  • 2020-11-27 11:13

    As https://maven.apache.org/plugins/maven-jar-plugin/examples/create-test-jar.html says:

    How to create a jar containing test classes When you want to create a jar containing test-classes, you would probably want to reuse those classes. There are two ways to solve this:

    1. The easy way Create an attached jar with the test-classes from the current project and loose its transitive test-scoped dependencies.

    2. The preferred way Create a separate project with the test-classes.

    Please read that article for detail.

    0 讨论(0)
提交回复
热议问题