Maven 使用正确的文件夹路径在 pom.xml 中添加 mainClass [英] Maven adding mainClass in pom.xml with the right folder path

查看:203
本文介绍了Maven 使用正确的文件夹路径在 pom.xml 中添加 mainClass的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用我的 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.

您需要配置 ma​​ven-jar-plugin 而不是 maven-compiler-plugin.jar-plugin 是负责打包和创建 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆