quote
1.编写POM
<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>HelloWorld</groupId>
<artifactId>HelloWorld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>HelloWorld</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.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>HelloWorld.HelloWorld.App</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2.编写主代码:
package HelloWorld.HelloWorld;
/**
* Hello world!
*
*/
public class App
{
public String sayHello()
{
return "Hello Maven";
}
public static void main( String[] args )
{
System.out.println( new App().sayHello() );
}
}
After the code is written, use Maven to compile, and run the command mvn clean compile
3 in the project root directory .Write test code:
package HelloWorld.HelloWorld;
import junit.framework.TestCase;
/**
* Unit test for simple App.
*/
public class AppTest extends TestCase
{
public void testSayHello() {
App app = new App();
String result = app.sayHello();
assertEquals( "Hello Maven",result );
}
}
Command: mvn clean test
4. Package and run
mvn clean package
mvn clean install
java -jar target\HelloWorld-0.0.1-SNAPSHOT.jar
结果:Hello Maven