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

前端 未结 6 1218
再見小時候
再見小時候 2021-02-12 21:16

My maven project intentionally only needs src/test/java and src/test/resources. After removing src/main/* folders, the expected w

相关标签:
6条回答
  • 2021-02-12 21:42

    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>
    
    0 讨论(0)
  • 2021-02-12 21:42

    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>
    
    0 讨论(0)
  • 2021-02-12 21:44

    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.

    0 讨论(0)
  • 2021-02-12 21:53

    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.

    0 讨论(0)
  • 2021-02-12 21:55

    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.

    0 讨论(0)
  • 2021-02-12 21:58

    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.

    0 讨论(0)
提交回复
热议问题