Configure Maven 3.6 and Springboot to build Java Web projects

The latest Maven 3.6 build Java Web project in conjunction with Spring boot is very convenient, just download Maven 3.6 to start building. First, mak...

The latest Maven 3.6 build Java Web project in conjunction with Spring boot is very convenient, just download Maven 3.6 to start building.

First, make sure that JDK is installed on the system:

[supervisor@localhost ~]$ java -version java version "1.8.0_201" Java(TM) SE Runtime Environment (build 1.8.0_201-b09) Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)

Download Maven 3.6.1:

[supervisor@localhost ~]$ wget http://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.6.1/binaries/apache-maven-3.6.1-bin.tar.gz --2019-04-15 20:04:34-- http://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.6.1/binaries/apache-maven-3.6.1-bin.tar.gz //The host mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)... 101.6.8.193, 2402:f000:1:408:8100::1: //Connecting mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)|101.6.8.193|:80... Connected. //HTTP request has been issued, waiting for response... 200 OK //Length: 9136463 (8.7M) [application/x-gzip] //Save to "apache-maven-3.6.1-bin.tar.gz" 100%[================================================================================================================================================================================================>] 9,136,463 391KB/s Use time 15 s 2019-04-15 20:04:49 (604 KB/s) - Preserved“ apache-maven-3.6.1-bin.tar.gz" [9136463/9136463])

Download the Maven installation package to / usr/java/

[supervisor@localhost ~]$ sudo mv apache-maven-3.6.1-bin.tar.gz /usr/java/. [supervisor@localhost java]$ cd /usr/java [supervisor@localhost java]$ sudo tar zxvf apache-maven-3.6.1-bin.tar.gz [supervisor@localhost java]$ sudo mv apache-maven-3.6.1 apache-maven #Change directory to apache-maven [supervisor@localhost java]$ sudo vim /etc/profile #Modifying environment variables

Add the following statement at the end of the / etc/profile file

78 #JAVA Environment 79 export JAVA_HOME=/usr/java/jdk1.8.0 80 export M2_HOME=/usr/java/apache-maven 81 export CLASSPATH=.:$/jre/lib/rt.jar:$/lib/dt.jar:$/lib/tools.jar 82 export PATH=$PATH:$/bin:$/bin 83
[supervisor@localhost java]$ su root [root@localhost java]$ source /etc/profile #Make environmental variable configuration take effect immediately [root@localhost java]$ exit [supervisor@localhost java]$ mvn -v #Successful installation of maven to display version information Apache Maven 3.6.1 (d66c9c0b3152b2e69ee9bac180bb8fcc8e6af555; 2019-04-05T03:00:29+08:00) Maven home: /usr/java/apache-maven Java version: 1.8.0_201, vendor: Oracle Corporation, runtime: /usr/java/jdk1.8.0/jre Default locale: zh_CN, platform encoding: UTF-8 OS name: "linux", version: "3.10.0-862.el7.x86_64", arch: "amd64", family: "unix"

Create a new project under the project catalogue and create subdirectories of the project:

[supervisor@localhost projects]$ mkdir myproject [supervisor@localhost projects]$ cd myproject/ [supervisor@localhost myproject]$ mkdir -p src/main/java/Exmaple

Create a new pom.xml file in the myproject directory

<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>com.Example</groupId> <artifactId>myproject</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
Enter the command mvn dependency:tree

maven will download related dependencies. Be careful not to modify the maven central warehouse to Aliyun. Some dependencies can not be found on Aliyun, and the project Build will fail.

Spring Boot introduces the concept of Starter. In this case, spring-boot-starter-web is a Starter, and Spring Boot will generate a Web application.

After a period of downloading, it finally shows that Build succeeded, listing the dependencies of Tree:

[INFO] Scanning for projects... [INFO] [INFO] -----------------------< com.Example:myproject >------------------------ [INFO] Building myproject 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-dependency-plugin:3.1.1:tree (default-cli) @ myproject --- [INFO] com.Example:myproject:jar:0.0.1-SNAPSHOT [INFO] \- org.springframework.boot:spring-boot-starter-web:jar:2.1.4.RELEASE:compile [INFO] +- org.springframework.boot:spring-boot-starter:jar:2.1.4.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot:jar:2.1.4.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot-autoconfigure:jar:2.1.4.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot-starter-logging:jar:2.1.4.RELEASE:compile [INFO] | | +- ch.qos.logback:logback-classic:jar:1.2.3:compile [INFO] | | | +- ch.qos.logback:logback-core:jar:1.2.3:compile [INFO] | | | \- org.slf4j:slf4j-api:jar:1.7.26:compile [INFO] | | +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.11.2:compile [INFO] | | | \- org.apache.logging.log4j:log4j-api:jar:2.11.2:compile [INFO] | | \- org.slf4j:jul-to-slf4j:jar:1.7.26:compile [INFO] | +- javax.annotation:javax.annotation-api:jar:1.3.2:compile [INFO] | +- org.springframework:spring-core:jar:5.1.6.RELEASE:compile [INFO] | | \- org.springframework:spring-jcl:jar:5.1.6.RELEASE:compile [INFO] | \- org.yaml:snakeyaml:jar:1.23:runtime [INFO] +- org.springframework.boot:spring-boot-starter-json:jar:2.1.4.RELEASE:compile [INFO] | +- com.fasterxml.jackson.core:jackson-databind:jar:2.9.8:compile [INFO] | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.0:compile [INFO] | | \- com.fasterxml.jackson.core:jackson-core:jar:2.9.8:compile [INFO] | +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.9.8:compile [INFO] | +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.9.8:compile [INFO] | \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.9.8:compile [INFO] +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.1.4.RELEASE:compile [INFO] | +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.17:compile [INFO] | +- org.apache.tomcat.embed:tomcat-embed-el:jar:9.0.17:compile [INFO] | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.17:compile [INFO] +- org.hibernate.validator:hibernate-validator:jar:6.0.16.Final:compile [INFO] | +- javax.validation:validation-api:jar:2.0.1.Final:compile [INFO] | +- org.jboss.logging:jboss-logging:jar:3.3.2.Final:compile [INFO] | \- com.fasterxml:classmate:jar:1.4.0:compile [INFO] +- org.springframework:spring-web:jar:5.1.6.RELEASE:compile [INFO] | \- org.springframework:spring-beans:jar:5.1.6.RELEASE:compile [INFO] \- org.springframework:spring-webmvc:jar:5.1.6.RELEASE:compile [INFO] +- org.springframework:spring-aop:jar:5.1.6.RELEASE:compile [INFO] +- org.springframework:spring-context:jar:5.1.6.RELEASE:compile [INFO] \- org.springframework:spring-expression:jar:5.1.6.RELEASE:compile [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 12.270 s [INFO] Finished at: 2019-04-15T20:42:09+08:00 [INFO] ------------------------------------------------------------------------

This process actually maven downloads and installs the spring,tomcat,log4j and related jboss tools and other related dependencies required by the project.

Next, create a new file src/main/java/Example.java

import org.springframework.boot.*; import org.springframework.boot.autoconfigure.*; import org.springframework.web.bind.annotation.*; @RestController @EnableAutoConfiguration public class Example { @RequestMapping("/") String home() { return "Hello World!"; } public static void main(String[] args) { SpringApplication.run(Example.class, args); } }

Enter mvn spring-boot:run in the myproject directory

[INFO] Scanning for projects... [INFO] [INFO] -----------------------< com.Example:myproject >------------------------ [INFO] Building myproject 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] >>> spring-boot-maven-plugin:2.1.4.RELEASE:run (default-cli) > test-compile @ myproject >>> [INFO] [INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ myproject --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory /alidata/projects/myproject/src/main/resources [INFO] skip non existing resourceDirectory /alidata/projects/myproject/src/main/resources [INFO] [INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ myproject --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ myproject --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory /alidata/projects/myproject/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ myproject --- [INFO] No sources to compile [INFO] [INFO] <<< spring-boot-maven-plugin:2.1.4.RELEASE:run (default-cli) < test-compile @ myproject <<< [INFO] [INFO] [INFO] --- spring-boot-maven-plugin:2.1.4.RELEASE:run (default-cli) @ myproject --- . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.4.RELEASE) 2019-04-15 20:57:38.187 INFO 438 --- [ main] Example : Starting Example with PID 438 (/alidata/projects/myproject/target/classes started by root in /alidata/projects/myproject) 2019-04-15 20:57:38.239 INFO 438 --- [ main] Example : No active profile set, falling back to default profiles: default 2019-04-15 20:57:44.743 INFO 438 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2019-04-15 20:57:45.021 INFO 438 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2019-04-15 20:57:45.022 INFO 438 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.17] 2019-04-15 20:57:46.409 INFO 438 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2019-04-15 20:57:46.410 INFO 438 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 7838 ms 2019-04-15 20:57:47.900 INFO 438 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2019-04-15 20:57:50.623 INFO 438 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2019-04-15 20:57:50.682 INFO 438 --- [ main] Example : Started Example in 14.93 seconds (JVM running for 33.934) 2019-04-15 20:58:09.252 INFO 438 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2019-04-15 20:58:09.253 INFO 438 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2019-04-15 20:58:09.294 INFO 438 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 41 ms

Open with a browser

16 April 2019, 01:18 | Views: 1022

Add new comment

For adding a comment, please log in
or create account

0 comments