34

I have a test-jar which I would like to install. I am not sure if there is a different way to install test-jars, such as defining a property that tells maven it is a test-jar.

Also, the groupId and artifactId are the same as another jar for which the test is made from.

So far this is how my install command looks like:

mvn install:install-file -DgroupId=com.example -DartifactId=example -Dpackaging=jar -Dversion=1.2.3 -Dfile=example-test.jar -DgeneratePom=true

So how exactly would I install a test jar? I know there has to be something to tell maven it is a test-jar since the groupId and artifactId is the same as another jar(which would be the jar that example-test.jar is a test of).

Sujen
  • 1,614
  • 5
  • 19
  • 25

4 Answers4

51

You don't need to install them manually. Maven will do this for you when executing:

mvn clean install

You need a configuration along the lines of:

    ...
    <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>
    ...

Then, later on in your other module where you'll need to use it, you need to define the dependency's type as:

 <dependency>
    <groupId>com.foo</groupId>
    <artifactId>bar</artifactId>
    <version>1.2.3</version>
    <type>test-jar</type>
    <scope>test</scope>
 </dependency>
carlspring
  • 31,231
  • 29
  • 115
  • 197
  • Right, but that assumes both projects are part of an aggregate POM, right? What happens when the project supplying the test-jar is completely separate from the project using the test-jar? I would want to first install the test-jar in the first project so that it would be in my local `~/.m2/repository`, right? How would I do that? – Garret Wilson Oct 25 '17 at 23:33
  • There is no mention of an aggregator in either the original poster's question, or my answer. You can have the two projects completely separate. You would still add the `test-jar` dependency the same way. All you care about is that the project producing the `test-jar` must be built before the project consuming it. – carlspring Oct 26 '17 at 11:26
  • Well if they are completely separate there must be some mechanism for one project to _find_ the test jars. But from my separate question https://stackoverflow.com/q/46943636/421049 it seems that using `mvn install` may actually install the test jar along with the main jars in the local repository, so that would make sense and allow other projects to find them. – Garret Wilson Oct 26 '17 at 21:59
12
mvn install:install-file 
    -DgroupId=com.example 
    -DartifactId=example 
    -Dversion=1.2.3 
    -Dclassifier=tests 
    -Dpackaging=test-jar 
    -Dfile=example-1.2.3-tests.jar
Vikdor
  • 23,934
  • 10
  • 61
  • 84
Rajani Karuturi
  • 3,450
  • 3
  • 27
  • 40
1

You put your test code in the same project as your normal code, under /src/test/java. Maven takes care of not including the test code in the packaged jar. If you have dependencies that are only used for unit testing (e.g. mockito, junit, etc) then you give them a scope of "test" in the maven dependencies and they'll be available to the unit tests but not included in the actual jar.

Kevin
  • 24,871
  • 19
  • 102
  • 158
0

I guess you might have missed the generatePom flag, i got the same error but finally the below worked

mvn install:install-file 
    -Dfile=c:/primo/primo-1.0.0-SNAPSHOT.jar 
    -DgroupId=uk.bl.primo 
    -DartifactId=primo 
    -Dversion=1.0.0 
    -Dpackaging=jar 
    -DgeneratePom=true
Vikdor
  • 23,934
  • 10
  • 61
  • 84
Mohammed Rafeeq
  • 2,586
  • 25
  • 26