一.Maven的package流程
有三种内置的build lifecycles:default、clean和site。默认生命周期处理项目部署,clean生命周期处理项目清理,而site生命周期处理项目站点文档的创建。
- A Build Lifecycle is Made Up of Phases.
输入命令时:
You should select the phase that matches your outcome. (package是default lifecycles下的一个Phase)
mvn package
- A Build Phase is Made Up of Plugin Goals
执行Phase其实就是执行相应的插件的目标。
maven-resources-plugin:3.2.0:resources(插件:版本:目标)
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ web-log ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 1 resource
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ web-log ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to D:\study\ideaProject\streaming-project\web-log\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ web-log ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory D:\study\ideaProject\streaming-project\web-log\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ web-log ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:\study\ideaProject\streaming-project\web-log\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ web-log ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.chengyanban.weblog.WebLogApplicationTests
[INFO]... //省略
[INFO] Results:
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ web-log ---
[INFO] Building jar: D:\study\ideaProject\streaming-project\web-log\target\web-log-0.0.1.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.4.4:repackage (repackage) @ web-log ---
[INFO] Replacing main artifact with repackaged archive
二.运行jar包常见错误
- 打胖包,所有依赖打到包里去(不是很推荐)
注意 <mainClass>util.Microseer</mainClass>这一行,不然运行会找不到主类。 用下面方法也可以解决。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration> <!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs> <!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<mainClass>util.Microseer</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
- 打瘦包,默认配置就是打瘦包,要先把依赖的包下载到linux指定目录下
注意:可能会报错jar中没有主清单属性
方法一:java -classpath [jar包名] [主类名] [参数]
java -classpath client-access-1.0.jar DataProducer http://hadoop001:8080/chengyanban-web/upload
方法二:打开jar包,修改MANIFEST.MF,加上 Main-Class: DataProducer
MANIFEST.MF:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: zla
Created-By: Apache Maven 3.6.3
Build-Jdk: 1.8.0_251
Main-Class: DataProducer
然后会再报错:java.lang.NoClassDefFoundError: com/alibaba/fastjson/JSONObject(打胖包也可以解决,但不推荐)
“…jar: …jar”:不同jar之间要用冒号隔开,不是分号。
java -classpath "client-access-1.0-SNAPSHOT.jar:./dependency-lib/fastjson-1.2.41.jar" DataProducer http://hadoop001:8080/chengyanban-web/upload
最方便的就是打胖包,然后主类没写错,可直接 java -jar 就可运行。但是不是很推荐
三.第三方jar包打入本地仓库
mvn install:install-file \
-Dfile=D:/study/access-log-0.0.1-SNAPSHOT.jar \
-DgroupId=com.chengyanban \
-DartifactId=log-gen \
-Dversion=1.0 \
-Dpackaging=jar
然后在pom文件中加上依赖,就可以使用了。
<dependency>
<groupId>com.chengyanban</groupId>
<artifactId>log-gen</artifactId>
<version>1.0</version>
</dependency>