k_rollo
k_rollo

Reputation: 5472

Maven - Suppress [WARNING] JAR will be empty - no content was marked for inclusion in pom.xml

My maven project intentionally only needs src/test/java and src/test/resources. After removing src/main/* folders, the expected warning showed up upon mvn verify:

[WARNING] JAR will be empty - no content was marked for inclusion!
[INFO] Building jar: D:\dev\java\my-project\target\my-project-0.0.1-SNAPSHOT.jar

How to suppress this warning apart from having a class with an empty main() method in src/main/java?

EDIT:

As -q suppresses the warning, a followup would be if this can be done programmatically in the pom.xml?

Upvotes: 19

Views: 13100

Answers (6)

neXus
neXus

Reputation: 2223

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.

A shorter alternative

Some of these configuration parameters are tied to user properties:

  • maven-jar-plugin's skipIfEmpty parameter sadly is not linked to any user property
  • maven-install-plugin's skip parameter gets its value from maven.install.skip
  • maven-deploy-plugin's skip parameter gets its value from maven.deploy.skip

This means the pom could be shortened to

    <properties>
        <maven.install.skip>true</maven.install.skip>
        <maven.deploy.skip>true</maven.deploy.skip>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <skipIfEmpty>true</skipIfEmpty>
                </configuration>
            </plugin>
        </plugins>
    </build>

Upvotes: 15

Alex Krauss
Alex Krauss

Reputation: 10393

None of the other solutions seemed particularly attractive to me, so my solution was to add a file src/main/resources/dummy containing the explanation:

Dummy file to avoid empty jar warning from Maven.

At least this does not produce any noise in the pom.xml, and there cannot be any misunderstandings about the purpose of the file. It produces a jar with exactly that file, but this was no problem in my case.

Upvotes: 3

John Camerin
John Camerin

Reputation: 722

If all you are trying to do is gather dependecies, you could add <packaging>pom</packaging> to change from the default jar packaging.

However, based on the context in your question, having src/test/java and src/test/resources folders leads me to believe you have a component that is just tests? Changing packaging to pom would stop the tests from being run from maven. If this is indeed your use case, it would seem that you are trying to skip the packaging. The best way to achieve that is to bind the plugin for the modules packaging to phase none, as there is no way to skip the package phase of the maven lifecycle. This is identified as Solution 1 above.

Upvotes: 1

k_rollo
k_rollo

Reputation: 5472

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>

Upvotes: 6

khmarbaise
khmarbaise

Reputation: 97399

The simply and best solution is to keep the test code in src/main/java yes this is not a typo. Furthermore if it is a test framework the test framework must have tests itself so it makes sense to locate the tests for the productive code from the user point of view into src/main/java and the tests in src/test/java.

From the user point of view it will be simply jar file where you should define the <scope>test</scope>.

This will solve all the issues without any supplemental configuration and changing any default and no violation of conventions.

Upvotes: -1

df778899
df778899

Reputation: 10931

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>

Upvotes: 9

Related Questions