How do I install a test-jar in maven?

浪子不回头ぞ 提交于 2019-11-28 09:38:48

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>
Rajani Karuturi
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

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.

Mohammed Rafeeq

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!