Maven jar-with-dependencies 没有 target\classes(构建工件)

Maven jar-with-dependencies without target\classes (build artifacts)

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

我的项目层次结构是:

project
  -src
     -main

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

我希望我的 jar 只包含 class 主要的 es 和依赖项,而不是 target\classes 内的资源或资源内的资源。

我该怎么做?

我正在使用 maven-assembly-plugin,像这样:

       <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,设置如下所述。它将与程序集插件一样工作。

 <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>

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

更新

使用mvn install -Pgenerate-shaded-jar

调用配置文件

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

中的资源