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

ぐ巨炮叔叔 提交于 2019-12-06 13:07:52

问题


I created a pom.xml to compile my project and package it as a jar, and indeed it comiples and the jar is created - problem is that i got a jar with both classes and java inside, and i only want the classes inside.

How do I lose the java files? I don’t need them.

this is the pom.xml that i created:

<build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <finalName>api-interfaces</finalName>
        <plugins>
        <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <id>make-a-jar</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            <configuration>
          <excludes>
            <exclude>*.properties</exclude>
              <exclude>*.xml</exclude>
               <exclude>sql/**</exclude>
                <exclude>META-INF/**</exclude>
                 <exclude>*.jar</exclude>
                  <exclude>*.java</exclude>
                   <exclude>default-configs/**</exclude>

          </excludes>
          </configuration>
        </plugin>
       </plugins>
    </build>


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/26083326/how-do-i-exclude-java-files-from-a-jar-in-maven-jar-plugin

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