Java Maven 使用正确的文件夹路径在 pom.xml 中添加 mainClass
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29920434/
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 adding mainClass in pom.xml with the right folder path
提问by NhatNienne
I want to get a working jar file with my maven project.
我想用我的 Maven 项目获取一个工作 jar 文件。
The build part is:
构建部分是:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.14</version>
<dependencies>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>6.4.1</version>
</dependency>
</dependencies>
<configuration>
<consoleOutput>true</consoleOutput>
<configLocation>${basedir}/src/test/resources/checkstyle_swt1.xml</configLocation>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptor>src/assembly/src.xml</descriptor>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
<addClasspath>true</addClasspath>
<mainClass>org.jis.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
So my problem right now is that I don'T know how to implement the mainclass properly into my pom.xml and later into my jar file. The Folder Structure is: src/main/java/org/jis/Main.java but if I add the following line
所以我现在的问题是我不知道如何将主类正确地实现到我的 pom.xml 中,然后再到我的 jar 文件中。文件夹结构是:src/main/java/org/jis/Main.java 但如果我添加以下行
<mainClass>src.main.java.org.jis.Main</mainClass>
it doesn't work.
它不起作用。
Thanks in advance
提前致谢
采纳答案by Martin Baumgartner
First, your main class doesn't include src/main/java
. Look at the package declaration in that Java file. For example, package org.jis;
, then add the main class to that. In other words, it's only org.jis.Main
.
首先,您的主类不包括src/main/java
. 查看该 Java 文件中的包声明。例如,package org.jis;
然后添加主类。换句话说,它只是org.jis.Main
.
You need to configure the maven-jar-plugininstead the of the maven-compiler-plugin. The jar-plugin is the one which is responsible for packaging and creating the manifest.MF.
您需要配置maven-jar-plugin而不是 maven-compiler-plugin。jar-plugin 负责打包和创建 manifest.MF。
From http://maven.apache.org/shared/maven-archiver/examples/classpath.html
来自http://maven.apache.org/shared/maven-archiver/examples/classpath.html
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
...
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
</configuration>
...
</plugin>
</plugins>
</build>
回答by Dagriel
You have to use the fully qualified name of your main class:
您必须使用主类的完全限定名称:
<mainClass>org.jis.Main</mainClass>
回答by tinku
Basically Maven is a framework which have a collection of maven-plugin for each and every action performed to build a project.
基本上,Maven 是一个框架,它为构建项目而执行的每个操作都有一组 maven-plugin。
Each Build Phase is performed by corresponding maven-plugin using project's description Project Object Model(POM)
每个构建阶段都由相应的 maven-plugin 使用项目的描述项目对象模型(POM)执行
So maven-compiler plugin is just responsible to compile the project and it won't create the manifest file.
所以 maven-compiler 插件只负责编译项目,它不会创建清单文件。
maven-jar-plugin is responsible for creating the manifest file of the project.
maven-jar-plugin 负责创建项目的清单文件。
回答by Piyush Verma
In case, You are using Spring Boot Application. Add the below in pom.xml
如果您使用的是 Spring Boot 应用程序。在 pom.xml 中添加以下内容
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
This will add the Main-Class entry in MANIFEST.MF file.
这将在 MANIFEST.MF 文件中添加 Main-Class 条目。
回答by Vinto
You may mention as below if it's a spring boot application.
如果它是 Spring Boot 应用程序,您可以在下面提及。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.test.MainClass</mainClass>
</configuration>
</plugin>
</plugins>
</build>