Maven - Suppress [WARNING] JAR will be empty - no content was marked for inclusion in pom.xml
The warning is actually based on whether it can find the configured <classesDirectory>
- by default target\classes
.
This means one simple way to bypass the warning is to point it at another deliberately empty directory:
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<classesDirectory>dummy</classesDirectory>
</configuration>
</plugin>
</plugins>
</build>
Alternatively, to avoid the need for the empty directory, exclude everything from another directory:
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<classesDirectory>src</classesDirectory>
<excludes>
<exclude>**</exclude>
</excludes>
</configuration>
</plugin>
Both solutions suppressed the warning message programmatically.
Solution 1:
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-jar-plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<!-- Ignore src/main in JAR packaging -->
<configuration>
<classesDirectory>src</classesDirectory>
<excludes>
<exclude>main</exclude>
</excludes>
</configuration>
</plugin>
Solution 2:
Since the above essentially created an empty JAR (and I did not really want to include test classes and test resources), I opted to "skip" the JAR creation instead.
What is the best way to avoid maven-jar?
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>default-jar</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
This is (according to me) the cleanest solution
for projects that don't contain production code but do contain tests:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<skipIfEmpty>true</skipIfEmpty>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
It indicates what you want:
- to skip trying to package "no production code" into a jar
- don't try to install/deploy the non-existing jar
leaving test execution untouched.
Like @John Camerin mentioned it is NOT advised to use <packaging>pom</packaging>
unless the ONLY thing your pom should do is gather dependencies.
Otherwise, if you have tests, they would be skipped without warning, which is not what you want.