Java 如何在 Eclipse 中从 Maven 项目创建 Jar 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31742344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create Jar file from Maven project in eclipse
提问by Questioner
I have a Maven project, but I am not familiar to Maven. Just I wanted to create an executable JAR file from this Maven project to use it in another project by eclipse. I appreciate any help.
我有一个Maven项目,但我对Maven不熟悉。只是我想从这个 Maven 项目创建一个可执行的 JAR 文件,以便在 eclipse 的另一个项目中使用它。我很感激任何帮助。
回答by kswaughs
To build jar From Eclipse, Right click on your maven project name then
要从 Eclipse 构建 jar,请右键单击您的 Maven 项目名称,然后
Run as > Maven install
运行方式 > Maven 安装
回答by Iker Aguayo
Command line approach:
命令行方法:
In the root of the project (the maven project), should be a pom.xml. Go to that root and run mvn package. If this is correct, there should be a new folder with the name targetin the root of the project. Inside this folder there should be the jar file.
在项目的根目录(maven 项目),应该是一个 pom.xml。转到该根目录并运行mvn package。如果这是正确的,项目根目录中应该有一个名为target的新文件夹。在这个文件夹中应该有 jar 文件。
回答by szefuf
First of all, you have to remember about security in Java. Many jars would not work in fatjars, if they got included in other projects (for example bouncycastle).
首先,您必须记住 Java 中的安全性。如果它们被包含在其他项目中(例如 bouncycastle),许多 jars 将无法在 fatjars 中工作。
If you are doing a simple executable jar that has no libs in it, and requires all of them on classpath, default build (when packageing tag is set to jar) would be ok, and just require a proper manifest.
如果您正在做一个没有库的简单可执行 jar,并且需要在类路径上的所有库,默认构建(当打包标记设置为 jar 时)就可以了,只需要一个正确的清单。
If you need all libs inside (fatjar), you need to configure it yourself.
如果需要里面的所有库(fatjar),需要自己配置。
There are several plugins for it, for example maven-shade-plugin
:
它有几个插件,例如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>
回答by JustCode
Right click maven project,
右键maven项目,
choose Run As-> Maven Build ....
选择 Run As-> Maven Build ....
Type packagein the Goalsbox.
在目标框中键入包。
Click Run.
单击运行。
回答by Dulith De Costa
Add following into pom.xml
file and Run as Maven Install
. This worked for me.
将以下内容添加到pom.xml
文件中并以Maven Install
. 这对我有用。
pom.xml
pom.xml
<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>
Now Run as Maven Install
.
现在运行为Maven Install
.
回答by jyotinadda
Install maven - https://maven.apache.org/download.cgi
安装 maven - https://maven.apache.org/download.cgi
Goto your project in eclipse Run -> Maven install
在 eclipse 运行 -> Maven install 中转到您的项目