简体   繁体   中英

java-maven2: How to include the a jar as depedency in pom so that I will be able to access test classes or classes in src/test/java folder

I have a set of functional jars(more than 3) that tests my source code. These jars just contains test classes and assisting asserter classes. I am creating a new performance jar that would import all the functional tests from these jars so that all can be run simultaneously. But when I include them as test dependencies in pom of current jar, what all I get to see is the classes in src/main/java. How can I include these functional jars as dependent jars so that I can also reference classes in src/test/java. In other words, how do I reference the test classes in other jars. In what way should I include the dependency as.

Thanks for your support.

First option

Put your common "test" code in a separate module (in src/main/java ) and declare a dependency on this module with a test scope in modules that need the common code:

<dependency>
  <groupId>my.group.id</groupId>
  <artifactId>testing-framework</artifactId>
  <version>${project.version}</version>
  <scope>test</scope>
</dependency>

That's a common practice, there is nothing wrong with that.

Second option

Use the Maven JAR Plugin and its jar:test-jar goal to build a JAR of the test classes for the current project as part of the package phase ( jar:test-jar binds itself by default on package ):

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

To use the attached test JAR that was created above, the recommended way is to specify a dependency on the main artifact 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>

See the discussion at thee bottom of Guide to using attached tests for why <type>test-jar</type> should be preferred over <classifier>tests</classifier> .

The code in src/test/java is not included in the jar by default (I consider this to be a good thing and would be cautious about changing it even if I could).

I find putting common test code into a separate maven module's src/main/java to be a very good practice. Then, just include this module as a test-scoped dependency where you need it.

Either go the way suggested by Lauri or use a slight variation where you don't have a dedicated test artifact but a commons artifact that also has common test classes in src/test/java.

In this project, use the test-jar mojo to create and attach the test jar of this project

Now in derived projects, you can include the dependency like this:

<dependency>
    <groupId>...</groupId>
    <artifactId>...</artifactId>
    <version>...</version>
    <scope>test</scope>
    <classifier>test</classifier>
</dependency>

that way you don't need to create a separate test project if you already have a commons project

mvn package doesn't jar up test classes and resources. So you'd have to put the dependencies into src/main/java.

Whenever I do this I create a jar project called "someproj-testutils" and have helper classes or base test classes and resources in the normal src/main/ locations and then reference someproj-testutils as a test scoped dep.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM