我的项目结构在 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 已正确创建,并且清单中提到了主类。我有以下问题:
目标文件夹包含包含类文件的类文件夹。罐子里也含有它们,那么为什么需要它们呢?我的目标是拥有一个仅包含所有依赖项的可执行 jar。
资源根本没有添加到 jar 中。我已经按照网上的说明添加了变压器,但没有用!
在目标文件夹中创建的其他目录是什么(maven-archiver、surefire、surefire-reports 等)?
每次我进行 Maven 全新安装时都会创建另一个 jar (original-Listener....jar)
我完全不知道如何包含资源。任何帮助表示赞赏!
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-dependency.jar,其中包含文件夹中引用的 jar 中的所有类。清单还包含主类。
问题仍然是我无法将资源包包含在文件夹 \src\main\resources 中。
另外,我无法理解为什么从我的代码引用的 jar 文件包含在 jar 中以及 META-INF 文件夹中。
问题不在于您的 maven-shade-plugin 的配置,而是您已明确将资源目录设置为错误的路径:
<!-- wrong -->
<resource>
<directory>resources</directory>
</resource>
As <directory>
element 的文档说:“描述存储资源的目录。路径是
相对于 POM."
因此,如果您遵循默认的 Maven 项目结构,则必须如下设置:
<!-- correct -->
<directory>src/main/resources</directory>
或者不设置它,然后它会回退到与 maven 默认值相同的值。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)