如何在eclipse中从Maven项目创建Jar文件

时间:2015-07-31 09:10:05

标签: java eclipse maven jar executable-jar

我有一个Maven项目,但我对Maven并不熟悉。我想从这个Maven项目创建一个可执行的JAR文件,以便在eclipse的另一个项目中使用它。我感谢任何帮助。

7 个答案:

答案 0 :(得分:17)

构建jar从Eclipse中,右键单击您的maven项目名称,然后

  

以>运行Maven安装

答案 1 :(得分:3)

命令行方法:

在项目的根目录(maven项目)中,应该是一个pom.xml。转到该根目录并运行 mvn package 。如果这是正确的,那么项目的根目录中应该有一个名为 target 的新文件夹。在这个文件夹里面应该有一个jar文件。

答案 2 :(得分:2)

首先,您必须记住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>

答案 3 :(得分:1)

将以下内容添加到pom.xml文件中,然后以Maven Install运行。这对我有用。

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>

现在以Maven Install的身份运行。

答案 4 :(得分:0)

右键单击maven项目,

选择Run As-> Maven Build ....

目标框中输入

单击“运行”。

参考:https://teck4world.blogspot.com/2017/10/creating-jar-deployment-package-using.html

答案 5 :(得分:0)

安装maven-https://maven.apache.org/download.cgi

在Eclipse中转到您的项目 运行-> Maven安装

答案 6 :(得分:0)

如果你想用终端来做它

 mvn package

和“BUILD SUCCESS”目录前的一行

enter image description here