【问题标题】:unable to create a uber jar with all resources included无法创建包含所有资源的 uber jar
【发布时间】:2017-02-14 12:00:34
【问题描述】:
<?xml version="1.0" encoding="UTF-8"?>
<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>
    <artifactId>JavaExeDemo</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>JavaExeDemo</name>
    <url>http://maven.apache.org</url>
    <properties>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.6.4</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.6.4</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>el-api</artifactId>
            <version>2.2</version>
        </dependency>

        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.5</version>
        </dependency>



        <!-- Hibernate EHCache API -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.0.4.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.0.4.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-ehcache</artifactId>
            <version>5.0.4.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.2.2.Final</version>
        </dependency>
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.controlsfx</groupId>
            <artifactId>controlsfx</artifactId>
            <version>8.40.10</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.12</version>
        </dependency>
        <!-- Test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <!-- add Main-Class to manifest file -->
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>application.Main</mainClass>
                                </transformer>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
                                    <resource>application/application.css</resource>
                                    <file>/testProject/src/main/java/application/application.css</file>
                                </transformer>
                            </transformers>
                            <outputDirectory>testProject/target/classes</outputDirectory>
                            <includeEmptyDirs>true</includeEmptyDirs>
                            <resources>
                                <resource>
                                    <directory>testProject/src/main/resources/ui/</directory>
                                    <filtering>false</filtering>
                                </resource>
                                <resource>
                                    <directory>testProject/src/main/java/com/xyz/internal/installer</directory>
                                    <filtering>false</filtering>
                                </resource>
                                <resource>
                                    <directory>testProject/src/main/java/application</directory>
                                    <filtering>false</filtering>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
        </plugins>
    </build>

    <groupId>test</groupId>
</project>

我是 Maven 新手,一直在尝试为我的应用程序创建一个 uber jar。我已经能够包含包含转换器中提到的 application.css (仅当资源标签“不”使用时)但是我有一些资源分散在项目中,路径如下:

  1. /src/main/resources/ui/
  2. /src/main/java/com/xyz/internal/installer

我试图将它们作为资源包含在代码中,但是里面的图像 /src/main/resources/ui/ 包括在内,但其他位置不包括在内。此外,当我不使用资源标签时,jar 中包含“application.css”,否则不包含。

【问题讨论】:

    标签: java maven maven-shade-plugin maven-resources-plugin uberjar


    【解决方案1】:

    您的 pom.xml 看起来不正确。您将 resource filtering 与 maven shade 插件配置混合在一起。

    你应该像下面这样配置你的 pom,

       <build>       
        <resources>         
            <resource>        
                <directory>${project.basedir}/src/main/resources/ui/</directory>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>${project.basedir}/src/main/java/xyz/internal/installer/</directory>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>${project.basedir}/src/main/java/application</directory>
                <filtering>false</filtering>
            </resource>
        </resources>
        <!... 0ther stuff like dependencies...... >
        <plugins>
         <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <transformers>
                        <!-- add Main-Class to manifest file -->
                        <transformer
                            implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                            <mainClass>application.Main</mainClass>
                        </transformer>
                        <transformer
                            implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
                            <resource>application/application.css</resource>
                                <file>${project.basedir}/src/main/java/application/application.css</file>
                        </transformer>
                    </transformers>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
          </plugins>
       </build>
    

    几分,

    1. 我使用了 ${project.basedir} 而不是 testproject/src/... 这是最佳实践,您不应该对项目名称进行硬编码。同样使用 ${project.build.directory} 作为目标目录。见this
    2. 如果要将资源过滤器中指定的资源复制到特定位置,则必须使用copy resource plugin
    3. include empty directory是复制资源插件的配置之一

    【讨论】:

      猜你喜欢
      • 2019-12-23
      • 2014-01-08
      • 2012-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-01
      • 2013-07-07
      相关资源
      最近更新 更多