Java Maven 和 Netbeans:如何构建项目并获得可执行的 jar?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22332745/
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
Maven and Netbeans: how do I build project and get executable jar?
提问by
I've just started standard Maven -> Java Application in Netbeans 7.4. It created "App" class (which has main method) by default. I also went to project's Properties->Run and set that class as a Main Class. I then built the project.
我刚刚在 Netbeans 7.4 中启动了标准 Maven -> Java 应用程序。默认情况下,它创建了“App”类(具有 main 方法)。我还去了项目的 Properties->Run 并将该类设置为 Main Class。然后我建立了这个项目。
In the project's directory, I got the "target" folder with a couple of jars inside. None of them is the executable one. How do I simply fix the problem and get the executable jar at the end of each build?
在项目的目录中,我得到了“target”文件夹,里面有几个 jars。它们都不是可执行文件。如何在每次构建结束时简单地解决问题并获取可执行 jar?
thanks.
谢谢。
采纳答案by taringamberini
This may be a little lack of integration between Netbeans and Maven.
这可能是 Netbeans 和 Maven 之间有点缺乏集成。
Doing "Properties->Run and set that Main Class" in the IDE doesn't make Maven to set the main class in the MANIFEST.MF
of the generated jar: you have to explicitly say to Maven which is the main class by adding the <mainClass>
tag to the configuration
of the maven-jar-plugin
.
在 IDE 中执行“属性-> 运行并设置该主类”不会使 MavenMANIFEST.MF
在生成的 jar 中设置主类:您必须通过将<mainClass>
标记添加到主类来明确告诉 Mavenconfiguration
的maven-jar-plugin
。
For example if your pom.xml
were:
例如,如果您pom.xml
是:
<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.taringamberini</groupId>
<artifactId>AppTest</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.taringamberini.apptest.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
than in the target
directory you would find the AppTest-1.0.0-SNAPSHOT.jar
, and you should be able to run:
而不是在target
您会找到的目录中AppTest-1.0.0-SNAPSHOT.jar
,您应该能够运行:
AppTest/target$java -jar AppTest-1.0.0-SNAPSHOT.jar
Hello World!