无法创建包含所有资源的超级jar

时间:2017-02-14 12:00:35

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

<?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的新手,并且一直在尝试为我的应用程序创建一个超级jar。我已经能够包含include变换器中提到的 application.css (仅当资源标记未被使用时)但是我在项目中分散了一些资源路径如下:

  1. / SRC /主/资源/ UI /
  2. / SRC /主/ JAVA / COM / XYZ /内部/安装
  3. 我试图将它们作为资源包含在代码中,但是内部的图像 包含/src/main/resources/ui/但其他位置不包括在内。此外,当我不使用资源标签时,&#39; application.css&#39;包含在jar中,否则不包括在内。

1 个答案:

答案 0 :(得分:0)

您的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是复制资源插件的配置之一