Java 如何使用 maven shade 插件向 jar 添加资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21679990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:03:17  来源:igfitidea点击:

How to add resources to jar using maven shade plugin

javamaven

提问by kavita

My Project structure has resources folder inside the src/main/ folder. The resources folder contains the file server.properties. My pom is as follows:

我的项目结构在 src/main/ 文件夹中有资源文件夹。资源文件夹包含文件 server.properties。我的pom如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.fde</groupId>
    <artifactId>Listener</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Listener</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <hibernate.version>3.6.10.Final</hibernate.version>
        <java.version>1.6</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- Hibernate dependencies START -->
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-ehcache</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.12.1.GA</version>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.9</version>
        </dependency>
        <!-- Hibernate dependencies END -->
    </dependencies>
    <build>
        <sourceDirectory>src/main/java</sourceDirectory>
        <resources>
            <resource>
                <directory>resources</directory>
            </resource>
        </resources>
        <testSourceDirectory>src/test/java</testSourceDirectory>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.2</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.fde.ListenerServer</mainClass>
                                </transformer>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
                                    <resource>resources</resource>
                                    <file>server.properties</file>
                                </transformer>
                            </transformers>
                            <artifactSet>
                                <excludes>

                                    <exclude>junit:junit</exclude>

                                </excludes>
                            </artifactSet>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>
</project>

The Jar is created properly and the main class is mentioned in the manifest. I have the following questions :

Jar 已正确创建,清单中提到了主类。我有以下问题:

  1. The target folder contains the classes folder whch has the class files. The jar also contains them so why are they needed. My goal is to have a executable jar with all the dependancies only.

  2. The resources are not getting added in the jar at all. I have added the transformer according to instructions seen on the net but no use!!!

  3. What are the other dir getting created in the target folder (maven-archiver, surefire, surefire-reports etc) ??

  4. Another jar gets created every time i do a maven clean install (original-Listener....jar)

  1. 目标文件夹包含类文件夹,其中包含类文件。罐子里也有它们,所以为什么需要它们。我的目标是拥有一个仅包含所有依赖项的可执行 jar。

  2. 资源根本没有添加到 jar 中。我根据网上看到的说明添加了变压器但没有用!!!

  3. 在目标文件夹(maven-archiver、surefire、surefire-reports 等)中创建的其他目录是什么??

  4. 每次我执行 maven 全新安装时都会创建另一个 jar(原始监听器....jar)

I have absolutely no clue about how to include resources. Any help is appreciated!!

我完全不知道如何包含资源。任何帮助表示赞赏!

EDIT:::

编辑:::

This is the tag i used for the maven-assembly-plugin:

这是我用于 maven-assembly-plugin 的标签:

 <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>attached</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
    <archive>
            <manifest>
              <mainClass>com.fde.ListenerServer</mainClass>
            </manifest>
          </archive>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
    </configuration>
  </plugin>

This created the Listener-1.0-SNAPSHOT-jar-with-dependencies.jar with all the classes from referred jars in the folders. The manifest also contains the main class.

这创建了 Listener-1.0-SNAPSHOT-jar-with-dependencies.jar,其中包含来自文件夹中引用 jar 的所有类。清单还包含主类。

The problem still is that I cant include the resource bundle in the folder \src\main\resources.

问题仍然是我无法在文件夹 \src\main\resources 中包含资源包。

Also I cant understand why jar files referenced from my code are included in the jar as well as inside the META-INF folder.

我也无法理解为什么从我的代码中引用的 jar 文件包含在 jar 中以及 META-INF 文件夹中。

采纳答案by kavita

I removed the following from the pom.xml and the property files were included at the root dir.

我从 pom.xml 中删除了以下内容,并且属性文件包含在根目录中。

<sourceDirectory>src/main/java</sourceDirectory>
    <resources>
      <resource>
        <directory>resources</directory>
      </resource>
    </resources>
    <testSourceDirectory>src/test/java</testSourceDirectory>

Still havent figured out why there is a repetition of the referred classes in the jar.

仍然没有弄清楚为什么在 jar 中重复引用的类。

回答by Daniel Hári

The problem wasn't in your maven-shade-plugin's configuration, rather that you have explicitly set resource directory to a wrong path:

问题不在于您的 maven-shade-plugin 的配置,而是您已明确将资源目录设置为错误的路径:

    <!-- wrong -->
    <resource>    
      <directory>resources</directory>
    </resource>

As <directory>element's doc says: "Describe the directory where the resources are stored. The path is relative to the POM."

正如<directory>element 的文档所说:“描述存储资源的目录。路径是相对于 POM 的。”

So if you follow default maven project structure you have to set like this:

所以如果你遵循默认的 maven 项目结构,你必须像这样设置:

    <!-- correct -->
    <directory>src/main/resources</directory>

or don't set it, then it falls back to same as maven defaults.

或者不设置它,然后它会回退到与 maven 默认值相同的状态。