How do I exclude java files from a jar in maven-jar-plugin?

纵饮孤独 提交于 2019-12-04 19:56:12

Here's the culprit:

    <resources>
        <resource>
            <directory>src/main/java</directory>
            <filtering>true</filtering>
        </resource>
    </resources>

With this, you explicitly specify that files in src/main/java, i.e. the .java files, should be included in the jar.

you can either use the standard maven layout and put resources in src/main/resources, or explicitly exclude .java files using this:

    <resources>
        <resource>
            <directory>src/main/java</directory>
            <filtering>true</filtering>
            <excludes>
                <exclude>**/*.java</exclude>
            </excludes>
        </resource>
    </resources>

See maven resource plugin for more infos, especially the include/exclude examples

Is there a good reason for binding the maven-jar-plugin:jar goal to the compile phase explicitly? The compile phase is intended to "compile the source code of the project", nothing else.

In a jar packaging project the jar:jar goal is bound to the package phase by default which "take the compiled code and package it in its distributable format, such as a JAR."

The resources:resouces goal is bound to the process-resources phase which "copy and process the resources into the destination directory, ready for packaging."

Binding default goals to non-default phases is more like working against Maven than with it.

By using src/main/resources as mentioned by @SillyFreak in his answer you probably don't need this build step definition in your POM at all.

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