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

自闭症网瘾萝莉.ら 提交于 2020-07-17 09:45:47

问题


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?


回答1:


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>



回答2:


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>



回答3:


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.




回答4:


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.




回答5:


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.




回答6:


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.



来源:https://stackoverflow.com/questions/51570630/maven-suppress-warning-jar-will-be-empty-no-content-was-marked-for-inclusi

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