Set up the project
In a project directory of your choosing, create the following subdirectory structure; for example, with mkdir -p src/main/java/hello
新建以下文件夹
用IDEA打开
Within the src/main/java/hello
directory, you can create any Java classes you want. To maintain consistency with the rest of this guide, create these two classes: HelloWorld.java
and Greeter.java
.
src/main/java/hello/HelloWorld.java
copy
package hello;
public class HelloWorld {
public static void main(String[] args) {
Greeter greeter = new Greeter();
System.out.println(greeter.sayHello());
}
}
src/main/java/hello/Greeter.java
copy
package hello;
public class Greeter {
public String sayHello() {
return "Hello world!";
}
}
Now that you have a project that is ready to be built with Maven, the next step is to install Maven.
安装完成run mvn -v看是否安装成功
Once you have downloaded the zip file, unzip it to your computer. Then add the bin folder to your path.
把maven文件中的bin文件夹copy到之前建立的mkdir -p文件夹路径下
Define a simple Maven build
在根目录下建立一个pom.xlm文档,copy代码到文档里
<?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>org.springframework</groupId>
<artifactId>gs-maven</artifactId>
<packaging>jar</packaging>
<version>0.1.0</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>hello.HelloWorld</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
打开terminal运行mvn -v
Build Java code
在terminal中run
mvn compile
Declare Dependencies
First, change HelloWorld.java to look like this:
src/main/java/hello/HelloWorld.java
添加<dependencies>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.2</version>
</dependency>
</dependencies>
到project项目里
Write a Test
First add JUnit as a dependency to your pom.xml, in the test scope:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
Then create a test case like this:
src/test/java/hello/GreeterTest.java
package hello;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.*;
import org.junit.Test;
public class GreeterTest {
private Greeter greeter = new Greeter();
@Test
public void greeterSaysHello() {
assertThat(greeter.sayHello(), containsString("Hello"));
}
}
https://blog.csdn.net/l18848956739/article/details/86300571?