Mavenの使用規則は構成よりも優れています
ディレクトリ | 用途 |
---|---|
$ {basedir} | pom.xmlとすべてのサブディレクトリを保存する |
$ {basedir} / src / main / java | プロジェクトのJavaソースコード |
$ {basedir} / src / main / resources | プロパティファイル、springmvc.xmlなどのプロジェクトリソース |
$ {basedir} / src / test / java | JUnitコードなどのプロジェクトのテストクラス |
$ {basedir} / src / test / resources | テストのためのリソース |
$ {basedir} / src / main / webapp / WEB-INF | Webアプリケーションファイルディレクトリ、web.xmlなどのWebプロジェクト情報、ローカル画像、jspビューページ |
$ {basedir} /ターゲット | パッケージ出力ディレクトリ |
$ {basedir} / target / classes | コンパイル出力ディレクトリ |
$ {basedir} / target / test-classes | コンパイル出力ディレクトリをテストする |
Test.java | Mavenは、命名規則を満たすテストクラスのみを自動的に実行します |
〜/ .m2 / repository | Mavenのデフォルトのローカルリポジトリディレクトリの場所 |
1. Mavenプロジェクトを作成する
// 创建项目根据目录
~/Desktop$ mkdir MySpringboot
// 按照上面maven的约定创建目录
~/Desktop$ mkdir -p MySpringboot/src/main/java
~/Desktop$ mkdir -p MySpringboot/src/main/resources
~/Desktop$ mkdir -p MySpringboot/src/test/java
~/Desktop$ mkdir -p MySpringboot/src/test/resources
// 在根目录下创建pom.xml文件
~/Desktop$ touch MySpringboot/pom.xml
// 创建好的maven项目目录结构
~/Desktop$ tree MySpringboot
MySpringboot
├── pom.xml
└── src
├── main
│ ├── java
│ └── resources
└── test
├── java
└── resources
7 directories, 1 file
2. Spring Boot関連の依存関係をインポートする
以下の構成をpom.xmlに追加します。
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wong</groupId>
<!--与项目名称一致-->
<artifactId>MySpringboot</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
spring-boot-starter-webシーンスターターは、Web開発に関連する依存関係をプロジェクトにインポートします。シーンスターターの詳細については、springbootシーンスターターを参照してください。
3.メインプログラムを記述します。SpringBootアプリケーションを起動します。
パッケージ名com.wong(実際にはディレクトリ)を作成します。
~/Desktop/MySpringboot$ mkdir -p src/main/java/com/wong
パッケージcom.wongにメインプログラムMainApplication.javaを作成します。
package com.wong;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @SpringBootApplication用于标注主程序
*/
@SpringBootApplication
public class MainApplication{
public static void main(String[] args){
// 启动Spring应用
SpringApplication.run(MainApplication.class,args);
}
}
4.ビジネス関連のコントローラーを作成する
ビジネスコントローラーを格納するパッケージをもう1つ作成します。
~/Desktop/MySpringboot$ mkdir -p src/main/java/com/wong/controller
コントローラHelloWorldController.javaを作成します。
package com.wong.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("world")
public class HelloWorldController{
@GetMapping("hi")
public String index(){
return "Hello World!";
}
}
5.テスト
mvn spring-boot:runコマンドを使用してプログラムを実行します。
~/Desktop/MySpringboot$ mvn spring-boot:run
[INFO] Scanning for projects...
...
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.5.RELEASE)
2020-03-24 22:13:45.941 INFO 20580 --- [ main] com.wong.MainApplication : Starting MainApplication on kyun-HP-348-G3 with PID 20580 (/home/kyun/Desktop/MySpringboot/target/classes started by kyun in /home/kyun/Desktop/MySpringboot)
2020-03-24 22:13:45.944 INFO 20580 --- [ main] com.wong.MainApplication : No active profile set, falling back to default profiles: default
2020-03-24 22:13:46.662 INFO 20580 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-03-24 22:13:46.670 INFO 20580 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-03-24 22:13:46.671 INFO 20580 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.31]
2020-03-24 22:13:46.716 INFO 20580 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-03-24 22:13:46.716 INFO 20580 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 720 ms
2020-03-24 22:13:46.852 INFO 20580 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-03-24 22:13:46.967 INFO 20580 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-03-24 22:13:46.969 INFO 20580 --- [ main] com.wong.MainApplication : Started MainApplication in 1.298 seconds (JVM running for 1.604)
ブラウザにhttp:// localhost:8080 / world / hiと入力します。
7.導入を簡素化する
warパッケージをリリースして、Tomcatコンテナに公開できます。jarパッケージを直接入力し、java -jar <name> .jarコマンドを使用して直接実行できます。
最初に、次のプラグインをpom.xmlに追加します。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
次に、次のパッケージングコマンドを実行します。
~/Desktop/MySpringboot$ mvn package spring-boot:repackage
入力したパッケージはターゲットディレクトリにあります。
~/Desktop/MySpringboot$ cd target
~/Desktop/MySpringboot/target$ ls
classes maven-status
generated-sources MySpringboot-1.0-SNAPSHOT.jar
generated-test-sources MySpringboot-1.0-SNAPSHOT.jar.original
maven-archiver test-classes
~/Desktop/MySpringboot/target$ java -jar MySpringboot-1.0-SNAPSHOT.jar
...
これで、Spring Bootプロジェクトを作成できました。