本文介绍了Maven jar-with-dependencies没有target\classes(构建工件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个可执行jar(包含我的代码的所有* .class)。
但是,我不希望Jar在编译期间包含我的src / main / resources路径中的资源。

I want to create an executable jar (with all the *.class of my code in it).However, I don't want the Jar to include the resources that during compilation are in my src/main/resources path.

我的项目层次结构是:

my project hierarchy is:

project
  -src
     -main

    -resources
        -resources_setting.xml
  -target
     -classes
       -resources_setting.xml

我希望我的jar只包含main和依赖项的类,而不是target \classes或inside resources内的资源。

I want my jar to include only the classes of main and the dependencies, not the resources inside target\classes or inside resources.

我该怎么办那个?

我正在使用maven-assembly-plugin,如下所示:

I am using maven-assembly-plugin, like this:

       <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>cqm.qa.Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>


推荐答案

出于捆绑目的,我通常使用 maven-shade-plugin ,设置如下所述。它将与汇编插件一样工作。

For bundling purpose, I usually used the maven-shade-plugin and the settings are described below. It will work same as assembly plugin.

 <profile>
    <id>generate-shaded-jar</id>
    <activation>
        <activeByDefault>false</activeByDefault>
    </activation>
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                 <excludes>
                   <exclude>**</exclude>
                 </excludes>
           </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                       <executions>
                           <execution>
                               <phase>package</phase>
                               <goals>
                                   <goal>shade</goal>
                               </goals>
                               <configuration>
                                   <transformers>
                                       <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                            <manifestEntries>
                                                <Main-Class>cqm.qa.Main</Main-Class>
                                                <Class-Path>.</Class-Path>
                                             </manifestEntries>
                                        </transformer>
                                    </transformers>
                                   <filters>
                                        <filter>
                                            <artifact>*:*</artifact>
                                             <excludes>
                                                <exclude>log4j.properties</exclude>
                                                <exclude>details.properties</exclude>
                                              </excludes>
                                         </filter>
                                    </filters>
                                </configuration>
                            </execution>
                        </executions>
                <configuration>
                    <finalName>cqm-full</finalName>
                </configuration>
            </plugin>
        </plugins>
    </build>
</profile>

在上面的配置中,我排除了 log4j.properties details.properties 来自最终jar,其依赖项名称为 cqm-full.jar

In the above configuration, I've excluded log4j.properties and details.properties from the final jar with dependencies with name cqm-full.jar

更新

使用 mvn install -Pgenerate-调用配置文件shaded-jar

现在来自 src / main / resources 的资源文件不会加入 cqm-full.jar 。如果在没有配置文件 mvn clean install 的情况下调用,您仍然可以查看jar中的资源

Now resource files from src/main/resources won't get added in cqm-full.jar. If invoked without profile mvn clean install, you can still view the resources in the jar

这篇关于Maven jar-with-dependencies没有target\classes(构建工件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 05:29