java - 没有目标\类的 Maven jar-with-dependencies(构建 Artifact )

标签 java maven jar maven-assembly-plugin

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

我的项目层次结构是:

project
  -src
     -main

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

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

我该怎么做?

我正在使用 maven-assemble-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中的资源

关于java - 没有目标\类的 Maven jar-with-dependencies(构建 Artifact ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32296811/

相关文章:

java - javax.faces.bean包的真正起源是什么?

Maven编译失败但存在依赖

java - 尝试使用 eclipse 创建可运行的 jar 文件时收到警告消息

java - 想要使用 Gradle 将存储库中的 .ebextensions (AWS Beanstalk) 目录复制到 Java jar 文件的根目录中

Java反向传播算法非常慢

java - Spring 3.1 Web 应用程序问题

java - 使用SD卡作为输入流(图像)android而不是内部存储器

java - Maven 对其他本地项目的多级依赖

java - 如何禁用 sql-maven-plugin 文件复制到临时文件夹?

java - Netbeans:如何自动将独立 Java 应用程序的 JAR 文件传输到远程位置