0

Had a requirement to make a separate project with only test cases. The project (built using Maven) doesn't contain any files in the source package; it just contains test cases in the test sources. Currently, packaging them into a test JAR using maven-jar-plugin; relevant code as follows:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    <executions>
        <execution>
            <phase>prepare-package</phase>
            <goals>
                <goal>test-jar</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <outputDirectory>${assembleDir}/lib</outputDirectory>
        <excludes>
            <exclude>**/*.properties</exclude>
            <exclude>**/*.json</exclude>
        </excludes> 
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <mainClass>com.companyname.project.TestRunner</mainClass>
                <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
            </manifest>
            <manifestEntries>
                <Class-Path>../conf/</Class-Path>
            </manifestEntries>
        </archive>
    </configuration>
</plugin>

When I build the project, an empty JAR file for the source package also gets created. How can I avoid this without stopping the packaging of the test JAR?

A desirable solution would be to prevent Maven from creating JAR for source package if it contains no files.


The Solution

As per this answer to the above redirected question, added

<execution>
    <id>default-jar</id>
    <phase/>
</execution>

to the <executions> section above.

Although, not the exact solution that I wanted but it works never the less.

Community
  • 1
  • 1
Bilesh Ganguly
  • 3,792
  • 3
  • 36
  • 58
  • 1
    `skipIfEmpty` would indeed not work because you also configure a customized manifest file, hence jar would not be empty by definition, regardless of sources/resources. Moreover, `skipIfEmpty` would also apply to the `test-jar` goal (if no manifest customization would be configured). Then the solution is already mentioned on [Stack Overflow](http://stackoverflow.com/questions/2188746/what-is-the-best-way-to-avoid-maven-jar). – A_Di-Matteo Jun 22 '16 at 09:18
  • @A.DiMatteo I require this manifest info for my test jar. – Bilesh Ganguly Jun 22 '16 at 09:19
  • 1
    then skip the `default-jar` execution as explained in the linked SO q/a, [this minimal answer would do the trick](http://stackoverflow.com/a/18754749/5606016) – A_Di-Matteo Jun 22 '16 at 09:22
  • @A.DiMatteo Yup, was looking at that only. It works. Thanks. – Bilesh Ganguly Jun 22 '16 at 09:23
  • 1
    Better is to move those test classes into the `src/main/java` or `src/main/resources` only and create a usual jar from it. And everywhere you use it make it `test`...No supplemental configuration etc. execution needed.. – khmarbaise Jun 22 '16 at 14:25

0 Answers0