问题
Is it possible to use Spring Boots Maven plugin command spring-boot:run when the parent POM of the project is using packaging mode POM because of its children?
I have multi module maven project with a "master" POM that is in it's turn a child of the Spring Boot Parent module. Looking something like this:
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>project</artifactId>
<packaging>pom</packaging>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.0.BUILD-SNAPSHOT</version>
<relativePath/>
</parent>
<modules>
<module>module1</module>
<module>module2</module>
<module>module3</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>com.example.module1.Application</start-class>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
This is basically our "master" POM which every child uses as it's parent. Now we want to perform the spring-boot:run command from the working directory this "master" POM is in. Problem is, this generates a ClassNotFoundException which is odd since module1 (where this Application class is located) is included in the POM and mentioned as a module.
Using a single module maven project and <packaging>jar</packaging> this compiles and runs the Application class so it is not Spring-Boot that is not working right here.
What do I have to change to get this working or is it simply not possible to use the spring-boot-maven-plugin plugin when dealing with multi module Maven projects?
Sidenote: My Application class / Module 1 has the other modules as dependencies so keep this in mind when answering the question. Any suggestions on how to improve this are very appreciated.
回答1:
It is not possible, as far as I know, because the plugin requires an executable jar to run.
The docs for the spring-boot plugin goal "run" says:
Description
Run an executable archive application
Packaging pom is will not create an executable. It will only generate a pom.xml.
Try to run mvn install and look at the artifact deployed to your local repository:
I just did for a module I have with packaging pom:
[INFO] Installing C:\projects\boot-parent\pom.xml to
C:\Users\...\repository\no\boot-parent\0.0.1-SNAPSHOT\boot-parent-0.0.1-SNAPSHOT.pom
As you can see the artifact is boot-parent-0.0.1-SNAPSHOT.pom.
Try putting the plugin-configuration in the pom for module1 which has the start-class (main-class) and has packaging jar.
Edit for your comment:
Your comment suggests that you have issues with your client side Maven installation. Have a look at this answer. And especially the answer given by @ChristianAchilli.
I use to solve this issue by deleting the corresponding failed to download artifact directory in my local repo. Next time I run the maven command the artifact download is triggered again. Therefore I'd say it's a client side setting.
Again, hope this helps!
Thomas
回答2:
Make one module more where is only your Application inside. If you insert dependencies to your modules you can start your module normal with spring-boot:run or export as jar.
See your parent only as container.
Hope that work for you
来源:https://stackoverflow.com/questions/27297308/spring-boot-with-pom-packaging-agregator