Maven在pom.xml中使用正确的文件夹路径添加mainClass [英] Maven adding mainClass in pom.xml with the right folder path
问题描述
我想在我的maven项目中获取一个可工作的jar文件.
I want to get a working jar file with my maven project.
构建部分是:
<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>
所以我现在的问题是我不知道如何正确地将主类实现到pom.xml中,然后再实现到jar文件中. 文件夹结构为:src/main/java/org/jis/Main.java 但是如果我添加以下行
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
<mainClass>src.main.java.org.jis.Main</mainClass>
它不起作用.
预先感谢
推荐答案
首先,您的主类不包含src/main/java
.查看该Java文件中的程序包声明.例如,package org.jis;
,然后向其中添加主类.换句话说,它只是org.jis.Main
.
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
.
您需要配置 maven-jar-plugin 而不是maven-compiler-plugin. jar插件是负责打包和创建manifest.MF的插件.
You need to configure the maven-jar-plugin instead the of the maven-compiler-plugin. The jar-plugin is the one which is responsible for packaging and creating the manifest.MF.
来自 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>
这篇关于Maven在pom.xml中使用正确的文件夹路径添加mainClass的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!