Maven:将资源包含在JAR中 [英] Maven: Include resources into JAR

查看:92
本文介绍了Maven:将资源包含在JAR中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些可怕的beaviour。我有以下Maven配置:

I have some terrible beaviour. I have the following Maven configuration:

<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>Test2Certificate</artifactId>
    <version>0.1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>com.test</groupId>
        <artifactId>build</artifactId>
        <version>1.0.0</version>
    </parent>

    <properties>
        <compile.java.version>1.7</compile.java.version>
        <file.product.version>0.1.0.0</file.product.version>
        <maven.build.timestamp.format>yyyy-MM-dd HH:mm:SSS</maven.build.timestamp.format>
    </properties>

    <pluginRepositories>
        <pluginRepository>
            <id>launch4j-xml-plugin-repo</id>
            <name>launch4j-xml-plugin Repository for Maven</name>
            <url>https://launch4j-xml-plugin.googlecode.com/svn/repo</url>
        </pluginRepository>
    </pluginRepositories>

    <build>
        <finalName>${project.artifactId}_${project.version}</finalName>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>buildnumber-maven-plugin</artifactId>
                <version>1.2</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>create</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <doCheck>false</doCheck>
                    <doUpdate>false</doUpdate>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}/target</outputDirectory>
                            <includeEmptyDirs>true</includeEmptyDirs>
                            <resources>
                                <resource>
                                    <targetPath>${basedir}/target/customize</targetPath>
                                    <directory>customize</directory>
                                    <filtering>false</filtering>
                                </resource>
                                <resource>
                                    <targetPath>${basedir}/target/resources</targetPath>
                                    <directory>resources</directory>
                                    <filtering>false</filtering>
                                </resource>
                                <resource>
                                    <targetPath>${basedir}/target/xml</targetPath>
                                    <directory>xml</directory>
                                    <filtering>false</filtering>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <appendAssemblyId>false</appendAssemblyId>
                    <archive>
                        <manifest>
                            <mainClass>com.dscsag.dsct2c.main.MainClass</mainClass>
                        </manifest>
                        <manifestEntries>
                            <SplashScreen-Image>com/dscsag/dsct2c/resources/icons/loader/splash_screen.png</SplashScreen-Image>
                            <SCM-Revision>${buildNumber}</SCM-Revision>
                            <Project-Version>${project.version}</Project-Version>
                            <Build-Date>${maven.build.timestamp}</Build-Date>
                            <Project-Name>${project.artifactId}_${project.version}</Project-Name>
                        </manifestEntries>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jarsigner-plugin</artifactId>
                <version>1.2</version>
                <executions>
                    <execution>
                        <id>sign</id>
                        <goals>
                            <goal>sign</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <keystore>src/main/resources/jarsigner/keystore/dsct2c.keystore</keystore>
                    <alias>dsct2c</alias>
                    <storepass>DSCTest2Certificate</storepass>
                    <keypass>DSCTest2Certificate</keypass>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.bluestemsoftware.open.maven.plugin</groupId>
                <artifactId>launch4j-plugin</artifactId>
                <version>1.5.0.0</version>
                <executions>
                    <execution>
                        <id>l4j-gui</id>
                        <phase>package</phase>
                        <goals>
                            <goal>launch4j</goal>
                        </goals>
                        <configuration>
                            <headerType>gui</headerType>
                            <outfile>target/${project.artifactId}_${project.version}.exe</outfile>
                            <jar>target/${project.artifactId}_${project.version}.jar</jar>
                            <errTitle>${project.artifactId} ${project.version} - Error</errTitle>
                            <icon>src/main/java/com/dscsag/dsct2c/resources/icons/DSCT2C.ico</icon>
                            <jre>
                                <path>jre7</path>
                                <minVersion>1.7.0_17</minVersion>
                                <initialHeapSize>128</initialHeapSize>
                                <maxHeapSize>512</maxHeapSize>
                            </jre>
                            <versionInfo>
                                <fileVersion>1.0.0.0</fileVersion>
                                <txtFileVersion>1.0</txtFileVersion>
                                <fileDescription>Tool to certificate third-party products/adaptor.</fileDescription>
                                <copyright>DSC Software AG</copyright>
                                <productVersion>${file.product.version}</productVersion>
                                <txtProductVersion>${file.product.version}</txtProductVersion>
                                <productName>${project.artifactId}</productName>
                                <internalName>${project.artifactId}</internalName>
                                <originalFilename>${project.artifactId}_${project.version}.exe</originalFilename>
                            </versionInfo>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.1</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.4.2</version>
        </dependency>
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.7.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.6</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.swinglabs.swingx</groupId>
            <artifactId>swingx-all</artifactId>
            <version>1.6.4</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>com.toedter</groupId>
            <artifactId>jcalendar</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>com.jgoodies</groupId>
            <artifactId>jgoodies-common</artifactId>
            <version>1.4.0</version>
        </dependency>
        <dependency>
            <groupId>com.jgoodies</groupId>
            <artifactId>jgoodies-looks</artifactId>
            <version>2.5.2</version>
        </dependency>
        <dependency>
            <groupId>dsct2c.help</groupId>
            <artifactId>pdf_render</artifactId>
            <version>0.9.1</version>
        </dependency>
        <dependency>
            <groupId>dsct2c.help</groupId>
            <artifactId>jh</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>dsct2c.help</groupId>
            <artifactId>hsviewer</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>dsct2c.help</groupId>
            <artifactId>dsct2c_help</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.8</version>
        </dependency>
        <dependency>
            <groupId>commons-net</groupId>
            <artifactId>commons-net</artifactId>
            <version>3.3</version>
        </dependency>
    </dependencies>

</project>

这创建了包含所有类的JAR fie,但是我在类路径中有一些资源应该是也包括在内。如果我在阶段前面留下参数 clean ,那么我的所有资源都会包含在内。如果我使用 mvn clean package 运行maven,则不包含任何资源。我的资源在 src / main / java / com / test / (不是通常的目录)下。

This creates my JAR fie containing all classes, but I have some resources in the class path which should be included also. If I leave the parameter clean in front of the phase package, all my resources are included. If I run maven with mvn clean package, there aren't any resources included. My resources are under src/main/java/com/test/ (not the usual directory).

推荐答案

copy-resources 目标配置中未正确定义资源的源目录。此外,outputDirectory元素将资源放在目标目录中,当 target / classes 默认打包时。试试这个配置:

The source directories for the resources are not defined correctly in the copy-resources goal configuration. Also, the outputDirectory element puts the resources in the target dir, when target/classes is what gets packaged by default. Try this config:

<configuration>
    <outputDirectory>${basedir}/target/classes</outputDirectory>
    <includeEmptyDirs>true</includeEmptyDirs>
    <resources>
        <resource>
            <directory>${basedir}/src/main/java/com/test/customize</directory>
            <filtering>false</filtering>
        </resource>
        <resource>
            <directory>${basedir}/src/main/java/com/test/resources</directory>
            <filtering>false</filtering>
        </resource>
        <resource>
            <directory>${basedir}/src/main/java/com/test/xml</directory>
            <filtering>false</filtering>
        </resource>
    </resources>
</configuration>

也就是说,您可以考虑将资源放入 $ {basedir} / src / main / resources ,如下所示:

That said, you might consider putting the resources in ${basedir}/src/main/resources, like so:

src
   main
      resources
         customize
         resources
         xml

然后,你可以删除额外的 maven-resources-plugin 完全配置,默认生命周期将正确处理资源。

Then, you could remove the extra maven-resources-plugin config entirely, the default lifecycle will process the resources correctly.

这篇关于Maven:将资源包含在JAR中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆