我有一个Maven项目,但我对Maven并不熟悉。我想从这个Maven项目创建一个可执行的JAR文件,以便在eclipse的另一个项目中使用它。我感谢任何帮助。
要构建jar,请从Eclipse中右键单击您的maven项目名称
运行方式> Maven安装
命令行方法:
在项目的根目录(maven项目)中,应该是一个pom.xml。转到该根并运行mvn包。如果这是正确的,那么在项目的根目录中应该有一个名为target的新文件夹。在这个文件夹里面应该有一个jar文件。
首先,您必须记住Java中的安全性。如果它们被包括在其他项目中(例如bouncycastle),许多罐子将无法在fatjars中工作。
如果你正在做一个没有libs的简单可执行jar,并且需要在classpath上使用它们,那么默认构建(当packageing标记设置为jar时)就可以了,只需要一个合适的清单。
如果你需要所有的libs(fatjar),你需要自己配置它。
它有几个插件,例如maven-shade-plugin
:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.RSA</exclude>
<exclude>META-INF/*.INF</exclude>
</excludes>
</filter>
</filters>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>my.package.MainClass</Main-Class>
<Class-Path>.</Class-Path>
</manifestEntries>
</transformer>
</transformers>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>fat</shadedClassifierName>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
将以下内容添加到pom.xml
文件中并运行为Maven Install
。这对我有用。
pom.hml
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.pohan.App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
现在运行Maven Install
。
右键单击maven项目,
选择Run As-> Maven Build ....
在“目标”框中键入包。
单击运行。
参考:https://teck4world.blogspot.com/2017/10/creating-jar-deployment-package-using.html