Java 使用 spring-boot 的 Maven 模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20731158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Maven module using spring-boot
提问by heldt
I like to configure my applications in maven by creating modules like;
我喜欢通过创建模块来在 maven 中配置我的应用程序;
<groupId>com.app</groupId>
<artifactId>example-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>app-api</module>
<module>app-impl</module>
<module>app-web</module>
</modules>
The modules then use the 'example-app' as the parent.
然后模块使用“example-app”作为父级。
Now I want use 'spring-boot' for my web application.
现在我想为我的 Web 应用程序使用“spring-boot”。
Is there a way to configure maven so that my 'app-web' is a spring-boot application?
有没有办法配置 maven 以便我的“app-web”是一个 spring-boot 应用程序?
The problem I'm facing is that you have to use spring-boot as a parent.
我面临的问题是你必须使用 spring-boot 作为父级。
采纳答案by Dave Syer
You don't have to use the spring-boot-starter-parent, it's just a way to get started quickly. All it provides are dependency management and plugin management. You can do both yourself, and you can use the spring-boot-dependencies (or equivalently the parent) to manage dependencies if you want a halfway step. To do that, use scope=import
like this
您不必使用 spring-boot-starter-parent,这只是一种快速入门的方法。它提供的只是依赖管理和插件管理。你可以自己做,如果你想要中途,你可以使用 spring-boot-dependencies(或等效的父级)来管理依赖项。为此,请scope=import
像这样使用
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<type>pom</type>
<version>1.0.2.RELEASE</version>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
回答by saljuama
Another alternative, is to include in the parent pom, the parent declaration for spring boot, as shown in this post
另一种选择是在父pom中包含spring boot的父声明,如this post所示
example-app pom.xml:
示例应用 pom.xml:
<project>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
// rest of the example-app pom declarations
</project>
After that, in the modules poms (app-web, app-impl, etc.), you declare example-app as parent, but now you can include the starter dependencies as you would normally do in a regular project.
之后,在模块 poms(app-web、app-impl 等)中,您将 example-app 声明为父级,但现在您可以像通常在常规项目中那样包含启动器依赖项。
app-web pom.xml:
app-web pom.xml:
<project>
<parent>
<groupId>org.demo</groupId>
<artifactId>example-app</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<name>app-web</name>
<artifactId>app-web</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.demo</groupId>
<artifactId>app-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.demo</groupId>
<artifactId>app-impl</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
// rest of the app-web pom declarations
</project>
Regarding version management, what i used in these examples aren't exactly the best practices, but since is out of the scope of the question i skipped dependencyManagement and parent properties usage.
关于版本管理,我在这些示例中使用的并不是最佳实践,但由于超出了问题的范围,我跳过了dependencyManagement 和父属性的使用。
Also, if there is a starter that is used in every module, you can declare the dependency in the parent pom and then all the modules will inherit it (for example spring-boot-starter-test)
另外,如果每个模块中都有一个 starter,你可以在父 pom 中声明依赖,然后所有模块都会继承它(例如spring-boot-starter-test)