我喜欢通过创建模块来配置maven中的应用程序;
com.app example-app 0.0.1-SNAPSHOT pom app-api app-impl app-web
然后,模块使用'example-app'作为父级.
现在我想在我的Web应用程序中使用'spring-boot'.
有没有办法配置maven,以便我的'app-web'是一个spring-boot应用程序?
我面临的问题是你必须使用spring-boot作为父级.
您不必使用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>
另一种替代方法,是在父POM,为弹簧引导父声明包括,如图此交
example-app 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>
之后,在模块poms(app-web,app-impl等)中,您将example-app声明为parent,但现在您可以像通常在常规项目中那样包含入门依赖项.
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>
关于版本管理,我在这些示例中使用的并不是最佳实践,但由于超出了问题的范围,我跳过了dependencyManagement和父属性使用.
此外,如果在每个模块中都使用了启动器,则可以在父pom中声明依赖关系,然后所有模块都将继承它(例如spring-boot-starter-test)