1
votes

Let's say that I put the following plugin in my pom.xml file:

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <executions>
          <execution>
            <id>make-a-jar</id>
            <phase>compile</phase>
            <goals>
              <goal>jar</goal>
            </goals>
            <configuration>
               <finalName>my-artifact-name</finalName>
            </configuration>
          </execution>
        </executions>
      </plugin>

and I run "mvn clean install". Maven creates two jar files in target library. The first jar file compiles the source files and the second jar compiles the test files. Both of these jar will have the same artifact name.

If I want to use the jar of source code as a dependency in another project, I can put the following dependency in the other project:

   <dependency>
      <groupId>groupId</groupId>
      <artifactId>my-artifact-name</artifactId>
      <scope>system</scope>
      <type>jar</type>
      <systemPath>${basedir}/lib/my-artifact-name.jar</systemPath>
   </dependency>

So far so good.

A problem arises if I also want to add a dependency for the test files. In this case I will have two dependencies with the same groupId and artifactId and different systemPath. Maven will not read two dependencies with same groupId and artifactId. Only one of them will be read.

One solution that I can think of is to make Maven to give a different artifact name for test. Do you know how to do it?

1
First of all, <systemPath> is not a recommended approach. If you mvn clean install one project, you don't need a system path to use it as dependency (on the same computer/account). Secondly, what is the purpose of your "second jar"? What should be in there? What is it used for?J Fabian Meier
one jar is for the source files and the second jar is for test filesCrazySynthax
Usually, you don't put test files into a jar. So why do you want that? What do you want to do with that jar?J Fabian Meier

1 Answers

2
votes

My general answer to that would be:

The tests in src/test/java are only for running them during the build. They need not be put into any jar.

If you need classes as helper classes for your tests, you can create a separate jar which contains these classes. This can then be used as test dependency.

In any case, try to avoid <systemPath>. If you build your project with mvn clean install on your computer, you can reference the resulting jar with a dependency like

   <dependency>
      <groupId>groupId</groupId>
      <artifactId>my-artifact-name</artifactId>
      <version>1.2.3</version>
   </dependency>

anywhere on the same account/computer without giving a <systemPath>.