Maven使用入门 HelloWorld

引用

  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() );
    }
}
代码编写完毕后,使用Maven进行编译,在项目根目录下运行命令mvn clean compile
3.编写测试代码:
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 );
    }
}
命令: mvn clean test

4.打包和运行
mvn clean package
mvn clean install

java -jar target\HelloWorld-0.0.1-SNAPSHOT.jar
结果:Hello Maven

猜你喜欢

转载自diaochenlong2.iteye.com/blog/1827220