Spring Boot 提供父 POM 以便更轻松地创建 Spring Boot 应用程序。
不是每个人都喜欢从 spring-boot-starter-parent
POM 继承来创建可执行的 jar/war。您可能需要使用自己的公司标准父级,或者您可能更喜欢显式声明所有 Maven 配置。在本教程中,我们将演示如何在没有父 pom 的情况下使用 Maven 创建可执行的 jar/war。
我们使用 Apache Maven 来构建和管理我们的项目依赖项。这次我们不继承自 spring-boot-starter-parent
,而是包含一个 dependencyManagement
BOM。使用此 BOM 时,我们需要包含 repackage
目标以创建可执行的 jar/war 文件。
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dailycodebuffer.example</groupId>
<artifactId>Spring-Boot-Custom-Parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Spring-Boot-Custom-Parent</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.dailycodebuffer.example.SpringBootCustomParen.SpringBootCustomParentApplication</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
接下来,我们可以开始简单地开始添加 Spring 依赖项并使用 Spring Boot 功能:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
为了说明这个例子,我们创建了一个简单的类,它将一些基本的输出打印到控制台。
@SpringBootApplication
public class SpringBootCustomParentApplication {
private static Logger log = LoggerFactory.getLogger(SpringBootCustomParentApplication.class);
public static void main(String[] args) {
SpringApplication.run(SpringBootCustomParentApplication.class, args);
}
@PostConstruct
private void init(){
log.info("creating an executable jar/war with spring boot without parent pom");
}
}
前面的应用程序生成以下输出。
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.0.RELEASE)
2019-11-02 00:59:11.383 INFO 7552 --- [ main] .d.e.S.SpringBootCustomParentApplication : Starting SpringBootCustomParentApplication on Shabbir with PID 7552 (E:\DailyCodeBuffer\Codes\Spring-MVC-Tutorials\Spring-Boot-Custom-Parent\target\classes started by shabb in E:\DailyCodeBuffer\Codes\Spring-MVC-Tutorials\Spring-Boot-Custom-Parent)
2019-11-02 00:59:11.431 INFO 7552 --- [ main] .d.e.S.SpringBootCustomParentApplication : No active profile set, falling back to default profiles: default
2019-11-02 00:59:15.580 INFO 7552 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-11-02 00:59:15.605 INFO 7552 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-11-02 00:59:15.606 INFO 7552 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.27]
2019-11-02 00:59:15.908 INFO 7552 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-11-02 00:59:15.908 INFO 7552 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 4107 ms
2019-11-02 00:59:16.019 INFO 7552 --- [ main] .d.e.S.SpringBootCustomParentApplication : creating an executable jar/war with spring boot without parent pom
2019-11-02 00:59:16.575 INFO 7552 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-11-02 00:59:17.029 INFO 7552 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-11-02 00:59:17.042 INFO 7552 --- [ main] .d.e.S.SpringBootCustomParentApplication : Started SpringBootCustomParentApplication in 6.993 seconds (JVM running for 9.981)
2019-11-02 00:59:42.356 INFO 7552 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-11-02 00:59:42.357 INFO 7552 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2019-11-02 00:59:42.382 INFO 7552 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 25 ms
在本快速教程中,我们了解了如何在没有父级 pom.xml 的情况下使用 Spring Boot。
示例的源代码可以在 over on GitHub 中找到。
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.dailycodebuffer.com/spring-boot-with-a-custom-parent/
内容来源于网络,如有侵权,请联系作者删除!