Spring Boot和Maven exec插件问题 [英] Spring boot and maven exec plugin issue

查看:93
本文介绍了Spring Boot和Maven exec插件问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个准Maven项目,除了pom.xml之外,它完全是空的.

I've created a bare-bone Maven project, completely empty except the pom.xml.

使用此pom(请注意,parent元素已被注释掉):

With this pom (note that parent element is commented-out):

<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>springboot-test</groupId>
  <artifactId>springboot-test</artifactId>
  <version>1.0.0-SNAPSHOT</version>

<!--    <parent> -->
<!--        <groupId>org.springframework.boot</groupId> -->
<!--        <artifactId>spring-boot-starter-parent</artifactId> -->
<!--        <version>1.1.9.RELEASE</version> -->
<!--    </parent> -->

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.2</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>

样品运行:

$ mvn exec:java -Dexec.mainClass=test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building springboot-test 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.3.2:java (default-cli) @ springboot-test ---
[WARNING] Warning: killAfter is now deprecated. Do you need it ? Please comment on MEXEC-6.
[WARNING]
java.lang.ClassNotFoundException: test
        at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:281)
        at java.lang.Thread.run(Thread.java:745)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.480 s
[INFO] Finished at: 2014-12-05T13:45:34-05:00
[INFO] Final Memory: 8M/176M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.3.2:java (default-cli) on project springboot-test: An exception occured while executing the Java class. test -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

这是预料之中的(此项目中没有小节test).

This is expected (there's no clas test in this project).

使用此pom(即,取消注释parent):

Using this pom (i.e. just un-commenting parent):

<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>springboot-test</groupId>
  <artifactId>springboot-test</artifactId>
  <version>1.0.0-SNAPSHOT</version>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.1.9.RELEASE</version>
  </parent>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.2</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>

样品运行:

$ mvn exec:java -Dexec.mainClass=test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building springboot-test 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) > validate @ springboot-test >>>
[INFO]
[INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) < validate @ springboot-test <<<
[INFO]
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ springboot-test ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.550 s
[INFO] Finished at: 2014-12-05T13:45:27-05:00
[INFO] Final Memory: 12M/178M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java (default-cli) on project springboot-test: The parameters 'mainClass' for goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java are missing or invalid -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginParameterException

exec-maven-plugin:1.3.2发生了同样的事情:

Same thing happens with exec-maven-plugin:1.3.2:

$ mvn org.codehaus.mojo:exec-maven-plugin:1.3.2:java -Dexec.mainClass=test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building springboot-test 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.3.2:java (default-cli) @ springboot-test ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.400 s
[INFO] Finished at: 2014-12-05T13:52:46-05:00
[INFO] Final Memory: 6M/157M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.3.2:java (default-cli) on project springboot-test: The parameters 'mainClass' for goal org.codehaus.mojo:exec-maven-plugin:1.3.2:java are missing or invalid -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginParameterException

其他信息:

$ java -version
java version "1.8.0_11"
Java(TM) SE Runtime Environment (build 1.8.0_11-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.11-b03, mixed mode)
$ mvn -v
Apache Maven 3.2.2 (45f7c06d68e745d05611f7fd14efb6594181933e; 2014-06-17T09:51:42-04:00)
Maven home: C:\dev\bin\apache-maven-3.2.2
Java version: 1.8.0_11, vendor: Oracle Corporation
Java home: C:\progra~1\java\jdk1.8.0_11\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "amd64", family: "dos"

问题:

  • 这为什么失败/如何解决?
  • 为什么添加parent时失败? exec-maven-plugin如何受到此影响?
  • Why is this failing / how to fix?
  • Why is this failing when parent is added? How is exec-maven-plugin influenced by this?

(作为一个附带说明,我喜欢Windows 7属于以下事实:"dos",我正在等待针对Windows 8.1实施的家族:"cp/m")

(As a side note, I love the fact that Windows 7 belongs to faimly: "dos", I'm waiting for family: "cp/m" to be implemented for Windows 8.1)

推荐答案

这是因为Spring-Boot在项目中添加了一些额外的Maven配置. 如果您使用的是日食,则有一个名为有效POM"的标签.

Its because Spring-Boot is adding some extra maven configuration to your project. If you are using eclipse, there's a Tab called: 'Effective POM'.

在此标签中,您可以看到它在下面添加了此代码段:

From this tab you can see that it adds this snippet below:

    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>exec-maven-plugin</artifactId>
      <version>1.2.1</version>
      <configuration>
        <mainClass>${start-class}</mainClass>
      </configuration>
    </plugin>

因此,当您以spring-boot作为父级运行它时,它将查找${start-class}的值,除非您定义了该值,否则它将解析为空,这就是当您看到有关mainClass没有值的错误的信息.参数.

So when you run it with spring-boot as parent, it looks for the value of ${start-class}, which resolves to empty unless you defined it, that's when you see the error about not having a value for the mainClass parameter.

将此添加到您的项目:

<properties>
    <start-class>com.Test</start-class>
</properties>

然后运行它:clean compile exec:java


[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ springboot-test ---
**********test**************
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.388 s
[INFO] Finished at: 2014-12-05T15:25:05-06:00
[INFO] Final Memory: 17M/231M
[INFO] ------------------------------------------------------------------------

您还可以在命令行上提供start-class属性的值,即使您未在pom文件中定义它,此方法也可以使用:

You can also provide the value for the start-class property on the command line, this works even if you didn't define it inside your pom file:

mvn exec:java -Dstart-class=com.Test

这篇关于Spring Boot和Maven exec插件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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