项目构建之maven篇:2.HelloWorld项目构建过程

文件结构说明:


项目构建之maven篇:2.HelloWorld项目构建过程

项目构建生命周期:


清理

编译

測试

打包

执行

部署

清理与编译


hello\pom.xml

POM:Project Object Model,项目对象模型

pom.xml与ant的build.xml类似

<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.demo.hello</groupId>
<artifactId>hello-world</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>hello</name>
</project>

说明:


modelVersion:指定当前POM模型的版本号,Maven2及Maven3仅仅能是4.0.0

groupId:项目组名称

artifactId:当前Maven项目在组中的唯一的id

version:版本号

hello\src\main\java下的Hello.java

package com.demo ;
public class Hello {
public void sayHi(){
System.out.println("hello world");
}
public static void main(String [] args){
new Hello().sayHi();
}
}

执行清理及编译命令:


进入hello的目录路径,执行

mvn clean compile

执行结果

项目构建之maven篇:2.HelloWorld项目构建过程

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd29iZW5kaWFua3Vu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

查看target目录的内容

项目构建之maven篇:2.HelloWorld项目构建过程

查看本地仓库

项目构建之maven篇:2.HelloWorld项目构建过程

測试:

hello\pom.xml


<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.demo.hello</groupId>
<artifactId>hello-world</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>hello</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

dependencies:指明这个项目所须要的依赖包

hello\src\test\java\HelloTest.java

package com.demo;

import org.junit.Test;

public class HelloTest {
@Test
public void testHello(){
new Hello().sayHi();
}
}

执行測试命令

mvn clean test

查看结果

项目构建之maven篇:2.HelloWorld项目构建过程

查看本地仓库

项目构建之maven篇:2.HelloWorld项目构建过程

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd29iZW5kaWFua3Vu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

打包

执行命令


mvn clean package

查看结果

项目构建之maven篇:2.HelloWorld项目构建过程

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd29iZW5kaWFua3Vu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

执行


又一次改造pom.xml,增加插件

pom.xml

<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.demo.hello</groupId>
<artifactId>hello-world</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>hello</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<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>com.demo.Hello</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

又一次执行打包命令:


mvn clean package

执行完成后,进入hello/target下,执行命令


java -jar hello-world-1.0.0-SNAPSHOT.jar

查看结果:

项目构建之maven篇:2.HelloWorld项目构建过程

安装到本地仓库,供其他项目依赖


执行命令


mvn clean install

查看本地仓库

项目构建之maven篇:2.HelloWorld项目构建过程

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd29iZW5kaWFua3Vu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

源码下载

上一篇:antd输入框只能输入数字且保留两位小数


下一篇:软件工程导论第六版 张海藩