【问题标题】:Maven jar-with-dependencies without target\classes (build artifacts)没有目标\类的Maven jar-with-dependencies(构建工件)
【发布时间】:2015-08-30 12:59:50
【问题描述】:

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

我的项目层次结构是:

project
  -src
     -main

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

我希望我的 jar 只包含 main 类和依赖项,而不是 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>

【问题讨论】:

  • 这是一个与您想要的类似的好解决方案。 stackoverflow.com/questions/3001713/…
  • 为什么你想这样做?如果这些文件不是你项目的资源,那么你不应该把它们放在src/main/resources下。
  • 我将它们用作资源,但我也在运行时编辑它们。所以查找和替换它的路径在 jar 文件(不是目录)内的 xml 文件是有问题的

标签: java maven jar maven-assembly-plugin


【解决方案1】:

出于捆绑的目的,我通常使用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>

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

更新

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

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

【讨论】:

  • 谢谢。以及如何排除 src/main/resources 下的 directory 内容进入 jar?不仅仅是所有 .xml,还有所有 *.properties.. *该路径中的所有文件
  • 由于您要删除所有src/main/resources,因此可以避免上述示例中的excludes 配置
猜你喜欢
  • 2012-01-31
  • 1970-01-01
  • 1970-01-01
  • 2011-07-13
  • 1970-01-01
  • 1970-01-01
  • 2011-06-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多