Maven简介
Maven 是一个项目管理工具,可以对 Java 项目进行自动化的构建和依赖管理。
下载maven
https://maven.apache.org/download.cgi
wget -c https://dlcdn.apache.org/maven/maven-3/3.8.6/binaries/apache-maven-3.8.6-bin.tar.gz
添加国内镜像
阿里云maven镜像库: https://developer.aliyun.com/mvn/guide
- 修改maven目录下的
settings.xml
文件
<mirror>
<id>aliyunmaven</id>
<mirrorOf>*</mirrorOf>
<name>阿里云公共仓库</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
- 修改项目
pom.xml
文件
<repositories>
<repository>
<id>aliyunmaven</id>
<url>https://maven.aliyun.com/repository/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
创建新项目
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
- DgroupId: 组织名,公司网址的反写 + 项目名称
- DartifactId: 项目名-模块名
- DarchetypeArtifactId: 指定 ArchetypeId,maven-archetype-quickstart,创建一个简单的 Java 应用
- DinteractiveMode: 是否使用交互模式
报错: java mvn package no main manifest attribute
解决:
- 使用
java -classpath
(-cp
)命令:java -cp xxx.jar my.packagepath.MainClass
OR pom.xml
添加设置:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<!-- <addClasspath>true</addClasspath> -->
<!-- <classpathPrefix>lib/</classpathPrefix> -->
<mainClass>cn.xjfme.encrypt.test.SecurityTestAll</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
OR
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
打包命令
mvn package
报错:
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 15 source files to /home/santic/SM2_SM3_SM4Encrypt/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] Source option 5 is no longer supported. Use 7 or later.
[ERROR] Target option 5 is no longer supported. Use 7 or later.
[INFO] 2 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
在maven的conf下的setting.xml中加上
<profile>
<id>jdk-1.8</id>
<activation>
<jdk>1.8</jdk>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
将jar依赖包打进jar包
正确示范:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>my.hangjin.App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
网上错误示范:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest><mainClass>com.wyz.Main</mainClass></manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
引用本地jar包
<dependency>
<groupId>com.dm</groupId>
<artifactId>DmJdbcDriver</artifactId>
<version>1.8.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/lib/DmJdbcDriver18.jar</systemPath>
</dependency>
不会被参与打包阶段
。
如果也想打包进去的话,需要在插件里做配置 <includeSystemScope>true</includeSystemScope>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- maven打包时会将外部引入的jar包(比如在根目录下或resource文件下新加外部jar包)打包到项目jar -->
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
Maven官网: https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
java -jar xx.jar是如何运行的 https://blog.csdn.net/qq_35785465/article/details/124490253
用Maven管理第三方jar包依赖 https://tianmingxing.com/2020/02/29/%E7%94%A8Maven%E7%AE%A1%E7%90%86%E7%AC%AC%E4%B8%89%E6%96%B9jar%E5%8C%85%E4%BE%9D%E8%B5%96/
maven配置阿里云镜像的两种方式 https://blog.csdn.net/shaoming314/article/details/120357151
Maven打包可执行jar包方法大全(史上最全) https://blog.csdn.net/londa/article/details/115098901