如何使用Maven Shade插件向Jar添加资源
我的项目结构的src/main/文件夹中有resources文件夹. resources文件夹包含文件server.properties.我的pom如下:
My Project structure has resources folder inside the src/main/ folder. The resources folder contains the file server.properties. My pom is as follows:
<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,并且清单中提到了主类.我有以下问题:
The Jar is created properly and the main class is mentioned in the manifest. I have the following questions :
-
目标文件夹包含具有类文件的classes文件夹.罐子里还装有它们,所以为什么需要它们.我的目标是拥有一个仅包含所有依赖关系的可执行jar.
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.
根本没有将资源添加到jar中.我已经按照网上的说明添加了变压器,但是没有用!!!
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!!!
在目标文件夹中创建的其他目录是什么(maven-archiver,surefire,surefire-reports等)?
What are the other dir getting created in the target folder (maven-archiver, surefire, surefire-reports etc) ??
每次我执行maven全新安装(原始监听器.... jar)时,都会创建另一个jar.
Another jar gets created every time i do a maven clean install (original-Listener....jar)
我绝对不知道如何包含资源.任何帮助表示赞赏!
I have absolutely no clue about how to include resources. Any help is appreciated!!
::
这是我用于maven-assembly-plugin的标签:
This is the tag i used for the 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中的所有类.清单还包含主类.
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.
问题仍然是我无法将资源束包含在文件夹\ src \ main \ resources中.
The problem still is that I cant include the resource bundle in the folder \src\main\resources.
我也无法理解为什么我的代码中引用的jar文件包含在jar以及META-INF文件夹内.
Also I cant understand why jar files referenced from my code are included in the jar as well as inside the META-INF folder.
我从pom.xml中删除了以下内容,并将属性文件包含在根目录中.
I removed the following from the pom.xml and the property files were included at the root dir.
<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.