Guangning Yu's Blog
Home
Code
Data
Setup
Industry
MachineLearning
Archive
Building Java Projects with Maven
2019-02-17 01:40:29
|
Java
Maven
### Create the directory structure ``` mkdir -p src/main/java/hello mkdir -p src/test/java/hello ``` ### Create classes `src/main/java/hello/HelloWorld.java` ``` package hello; import org.joda.time.LocalTime; public class HelloWorld { public static void main(String[] args) { LocalTime currentTime = new LocalTime(); System.out.println("The current local time is: " + currentTime); Greeter greeter = new Greeter(); System.out.println(greeter.sayHello()); } } ``` `src/main/java/hello/Greeter.java` ``` package hello; public class Greeter { public String sayHello() { return "Hello world!"; } } ``` ### Write a test `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")); } } ``` ### Create `pom.xml` `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/maven-v4_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> <dependencies> <!-- tag::joda[] --> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.9.2</version> </dependency> <!-- end::joda[] --> <!-- tag::junit[] --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- end::junit[] --> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.1</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> ``` ### Build Java code Generate compiled `.class` files in the `target/classes` directory: ``` mvn compile ``` Run test: ``` mvn test ``` Compile Java code, run any tests, and finish by packaging the code up in a JAR file within the `target` directory: ``` mvn package ``` Install project’s JAR file to a repository of dependencies on the local machine (usually in `.m2/repository` directory in the home directory): ``` mvn install ```
Previous:
test jms
Next:
Crontab basics