除了上文中(https://blog.csdn.net/solinger/article/details/100920866)提到的ant外,现在用的比较多的java的项目构建和管理的工具还有maven。
maven和ant相比较,多了一个叫做约定大于配置的概念,被maven管理的项目,其目录结构和可能的构建的生命周期都是被定义好的,这样做的好处使得目录结构和操作等符合规律和统一标准。
而ant则较为灵活,可以自己指定许多配置。则本次让我们是用maven来进行项目一个简单的java项目的配置和构建。
1. 在linux下安装maven
[wlin@wlin ~]$ history | tail -n 4
10056 2019-09-17 18:33:28 wget http://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.6.2/binaries/apache-maven-3.6.2-bin.tar.gz
10057 2019-09-17 18:33:37 gunzip apache-maven-3.6.2-bin.tar.gz
10058 2019-09-17 18:33:46 tar -xvf apache-maven-3.6.2-bin.tar
10065 2019-09-17 18:34:45 sudo ln -s /opt/apache-maven-3.6.2/bin/mvn /usr/local/bin/
[wlin@wlin ~]$ mvn --version
Apache Maven 3.6.2 (40f52333136460af0dc0d7232c0dc0bcf0d9e117; 2019-08-27T23:06:16+08:00)
Maven home: /opt/apache-maven-3.6.2
Java version: 1.8.0_201, vendor: Oracle Corporation, runtime: /usr/java/jdk1.8.0_201-amd64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-870.el7.csb.input.5.x86_64", arch: "amd64", family: "unix"
2. ant使用build.xml来进行组织构建,maven使用pom.xml。让我们不使用maven提供的模版,手动创建java项目并利用maven进行构建和管理。
[wlin@wlin try_mvn]$ cat 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>com.example.cara</groupId>
<artifactId>first-maven</artifactId>
<packaging>jar</packaging>
<version>0.1.0</version>
<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>
3. 使用mvn compile编译
[wlin@wlin try_mvn]$ mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------< com.example.cara:first-maven >--------------------
[INFO] Building first-maven 0.1.0
[INFO] --------------------------------[ jar ]---------------------------------
最后看到如下信息,则表示成功
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /home/wlin/try_mvn/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:04 min
[INFO] Finished at: 2019-09-17T18:48:29+08:00
[INFO] ------------------------------------------------------------------------
则我们看到有相应的class文件生成。
[wlin@wlin try_mvn]$ tree
.
├── pom.xml
├── src
│ └── main
│ └── java
│ └── hello
│ └── HelloWorld.java
└── target
├── classes
│ └── hello
│ └── HelloWorld.class
└── maven-status
└── maven-compiler-plugin
└── compile
└── default-compile
├── createdFiles.lst
└── inputFiles.lst
4. 使用mvn package进行打包
[wlin@wlin try_mvn]$ tree
.
├── pom.xml
├── src
│ └── main
│ └── java
│ └── hello
│ └── HelloWorld.java
└── target
├── classes
│ └── hello
│ └── HelloWorld.class
├── first-maven-0.1.0.jar
├── maven-archiver
│ └── pom.properties
├── maven-status
│ └── maven-compiler-plugin
│ └── compile
│ └── default-compile
│ ├── createdFiles.lst
│ └── inputFiles.lst
└── original-first-maven-0.1.0.jar
则我们看到有相应的jar包生成
则执行该jar包,可获得我们希望的输出
[wlin@wlin try_mvn]$ java -jar target/first-maven-0.1.0.jar
Hello World!
我们也可以使用mvn相关的指令运行我们的目标类文件
[wlin@wlin try_mvn]$ mvn exec:java -Dexec.mainClass="hello.HelloWorld"
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------< com.example.cara:first-maven >--------------------
[INFO] Building first-maven 0.1.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ first-maven ---
Hello World!
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.492 s
[INFO] Finished at: 2019-09-17T19:12:59+08:00
[INFO] ------------------------------------------------------------------------
5.使用mvn install将项目的jar和pom发布到mvn仓库中
[INFO] Installing /home/wlin/try_mvn/target/first-maven-0.1.0.jar to /home/wlin/.m2/repository/com/example/cara/first-maven/0.1.0/first-maven-0.1.0.jar
[INFO] Installing /home/wlin/try_mvn/pom.xml to /home/wlin/.m2/repository/com/example/cara/first-maven/0.1.0/first-maven-0.1.0.pom
6. 使用mvn deploy部署jar到远程仓库
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project first-maven: Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
由于我们没有配置远程仓库,所以报错
7. 使用mvn clean进行clean
[wlin@wlin try_mvn]$ mvn clean
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------< com.example.cara:first-maven >--------------------
[INFO] Building first-maven 0.1.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ first-maven ---
[INFO] Deleting /home/wlin/try_mvn/target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.355 s
[INFO] Finished at: 2019-09-17T18:59:22+08:00
[INFO] ------------------------------------------------------------------------
[wlin@wlin try_mvn]$ tree
.
├── pom.xml
└── src
└── main
└── java
└── hello
└── HelloWorld.java
4 directories, 2 files
另外:没有‘mvn run’这个lifecycle。mvn jetty:run是在jetty容器里运行服务。
更完成的mvn build lifecycle可参考官网https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html