简体   繁体   English

Maven 使用正确的文件夹路径在 pom.xml 中添加 mainClass

[英]Maven adding mainClass in pom.xml with the right folder path

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.所以我现在的问题是我不知道如何将主类正确地实现到我的 pom.xml 中,然后再到我的 jar 文件中。 The Folder Structure is: src/main/java/org/jis/Main.java but if I add the following line文件夹结构是:src/main/java/org/jis/Main.java 但如果我添加以下行

<mainClass>src.main.java.org.jis.Main</mainClass>

it doesn't work.它不起作用。

Thanks in advance提前致谢

First, your main class doesn't include src/main/java .首先,您的主类不包括src/main/java Look at the package declaration in that Java file.查看该 Java 文件中的包声明。 For example, package org.jis;例如, package org.jis; , then add the main class to that. ,然后添加主类。 In other words, it's only org.jis.Main .换句话说,它只是org.jis.Main

You need to configure the maven-jar-plugin instead the of the maven-compiler-plugin.您需要配置maven-jar-plugin而不是 maven-compiler-plugin。 The jar-plugin is the one which is responsible for packaging and creating the manifest.MF. 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>

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>

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 负责创建项目的清单文件。

您必须使用主类的完全限定名称:

<mainClass>org.jis.Main</mainClass>

It seems that some time has passed since the question but I'll share the solution I've reached for future readings.问题似乎已经过去了一段时间,但我将分享我为未来阅读而达成的解决方案。

I'm using a Maven Project in the IntellJ IDE.我在 IntellJ IDE 中使用 Maven 项目。 Even using the full path copied from the operation system like: C:\\Users\\Rogerio\\Desktop\\Script\\src\\main\\java\\Controller\\MainClass.java, with or without .java or even trying the com.src... approach the main class was not found while creating the jar file for my application.即使使用从操作系统复制的完整路径,如:C:\\Users\\Rogerio\\Desktop\\Script\\src\\main\\java\\Controller\\MainClass.java,有或没有 .java 甚至尝试 com.src ...在为我的应用程序创建 jar 文件时找不到主类。

The solution was putting the CDATA tag inside the mainClass tag of the maven-jar plugin, as follows:解决方案是将 CDATA 标签放在 maven-jar 插件的 mainClass 标签中,如下所示:

My file structures:我的文件结构:

[A directory imagem structure on IntelliJ][1] [1]: https://i.stack.imgur.com/LHCWc.jpg [IntelliJ 上的目录图像结构][1] [1]:https://i.stack.imgur.com/LHCWc.jpg

Inside my maven-jar plugin on pom.xml:在 pom.xml 上我的 maven-jar 插件中:

`<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass><![CDATA[Controller.MainClass]]></mainClass>
                    </manifest>
                </archive>
            </configuration>

` `

Just leaving this here for individuals who were as frustrated as I was trying to get an incredibly simple example to work.只是把这个留在这里给那些像我试图让一个非常简单的例子工作一样沮丧的人。

The issue I was having is that the maven jar archiver had some bad information cached, so if you are giving it the proper main class (or in my case you don't actually need to do that with the proper maven version and settings).我遇到的问题是 maven jar 归档器缓存了一些错误的信息,所以如果你给它合适的主类(或者在我的情况下,你实际上不需要使用合适的 maven 版本和设置来做)。

Try blowing away the "target" directory if you are using IntelliJ and have one.如果您使用 IntelliJ 并且有一个,请尝试清除“目标”目录。 The "target" directory being the one maven default places the jar for instance.例如,“目标”目录是一个 maven 默认放置 jar。

In case, You are using Spring Boot Application.如果您使用的是 Spring Boot 应用程序。 Add the below in pom.xml在 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 条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM