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

时间:2014-02-10 14:34:53

标签: java maven

我的Project结构在src / main /文件夹中有resources文件夹。 resources文件夹包含文件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>

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

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

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

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

  4. 每次进行maven clean install时都会创建另一个jar(original-Listener .... jar)

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

    EDIT :::

    这是我用于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>
    

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

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

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

2 个答案:

答案 0 :(得分:5)

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

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

仍然没有弄清楚为什么罐子里重复了被引用的类。

答案 1 :(得分:4)

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

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

由于<directory>元素的文档说:“描述资源存储的目录。路径是  相对于POM 。“

因此,如果您遵循默认的maven项目结构,则必须设置如下:

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

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