maven配置及使用(整理)
目录
参考官网:https://maven.apache.org/index.html
Maven介绍
- Maven是一筐服务于Java平台的自动化构建工具(本身是Java写的)
- 开发过程中,所有的路径或配置文件中配置的类路径等都是以编译结果的目录结构为标准的(运行时环境)
- 依赖:
- compile对所有文件编译
- test,只对test有效,不打包,不部署,比如junit
- provided,对程序有效,不打包,不部署,需要idea设置运行时包含provided的依赖
总结:
- compile用于编译和测试,test只用于测试,provided用于编译和测试,不打包
- 本地项目使用mvn install后会把项目打包放入本地仓库,这样其他依赖本项目的项目就可以在pom中添加依赖使用了
- provided是在开发时使用,但是在部署时忽略,在运行时由服务器提供
依赖传递:路径最短原则,先声明者优先原则
继承:
- 父工程统一管理版本,打包方式必须为pom
- 子工程声明父工程时添加relativePath,指明父工程pom的路径
- install时要先安装父工程
聚合:
- 父工程中配置
<modules>
指明子工程的相对路径,安装父工程时会自动安装子工程
Maven安装与配置
安装
- 安装好java环境,配置好
JAVA_HOME
- 下载maven安装包,解压到无空格非中文目录,配置环境变量
MAVEN_HOME
,配置%MAVEN_HOME%/bin
到path - 检查安装:
mvn -v
修改本地仓库地址
- 修改maven安装目录下conf/settings.xml,也可以在idea配置中覆盖
<localRepository>E:\maven\MavenRepository</localRepository>
配置远程仓库地址
<!--阿里云公共仓库https://maven.aliyun.com-->
<mirror>
<id>aliyunmaven</id>
<mirrorOf>*</mirrorOf>
<name>阿里云公共仓库</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
<!-- 阿里云仓库 -->
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>
<!-- 中央仓库1 -->
<mirror>
<id>repo1</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://repo1.maven.org/maven2/</url>
</mirror>
<!-- 中央仓库2 -->
<mirror>
<id>repo2</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://repo2.maven.org/maven2/</url>
</mirror>
idea中maven配置
File | Settings | Build, Execution, Deployment | Build Tools | Maven
,配置HOME目录和settings.xml文件
maven变量
来自maven默认pom文件 superpom
<directory>${project.basedir}/target</directory>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<finalName>${project.artifactId}-${project.version}</finalName>
<testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<scriptSourceDirectory>${project.basedir}/src/main/scripts</scriptSourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
maven命令
- 检查依赖:
mvn -U dependency:tree
- 更新依赖:
mvn -U idea:idea
- 编译打包:
mvn -Dmaven.test.skip=true clean install
maven打包
注意:idea在创建子模块时会自动在父模块的pom里面添加<packaging>pom</packaging>
,父工程作为聚合其他工程的工程,无法打包为jar,所以如果有子工程,必须将父工程的代码迁移到子工程
springboot项目使用spring-boot-maven-plugin打包可执行jar
点击查看代码
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>jar-with-dependencies</classifier>
<!--排除所有该groupId的依赖-->
<excludeGroupIds>org.xxx</excludeGroupIds>
<excludes>
<excludes>
<!--排除某个依赖-->
<exclude>
<groupId>org.xxx</groupId>
<artifactId>xxx</artifactId>
</exclude>
</excludes>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
使用maven-assembly-plugin打包为可执行jar
点击查看代码
<!--maven-assembly-plugin打包成可执行jar-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<mainClass>org.org.example.MainApplication</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!--重命名为xxx-jar-with-dependencies.jar-->
<!-- <finalName>${project.artifactId}-${project.version}-xxx</finalName>-->
<!--设为false表示不使用jar-with-dependencies作为后缀-->
<!-- <appendAssemblyId>false</appendAssemblyId>-->
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
依赖打入lib,配置打入conf,封装为tar.gz
- maven-resources-plugin
- maven-jar-plugin
- maven-dependency-plugin
- maven-assembly-plugin
点击查看代码
<build>
<plugins>
<!--maven-resources-plugin 拷贝resources资源-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
<executions>
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<!--要拷贝到哪个目录-->
<outputDirectory>${project.build.directory}/conf</outputDirectory>
<resources>
<!--拷贝哪些资源-->
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<!--maven-jar-plugin 配置生成的jar包元数据-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<!--排除resources下的文件打包到jar包 如果不排除会放在jar包里面-->
<excludes>
<exclude>*.yml</exclude>
<exclude>*.yaml</exclude>
<exclude>*.xml</exclude>
<exclude>*.properties</exclude>
</excludes>
<archive>
<manifest>
<!--是否添加依赖的jar包路径到jar包的元数据MANIFEST.MF-->
<addClasspath>true</addClasspath>
<!--每个jar依赖的前缀-->
<classpathPrefix>lib/</classpathPrefix>
<!--依赖的版本号不是带时间戳的-->
<useUniqueVersions>false</useUniqueVersions>
<!--设置jar包的主类名-->
<mainClass>org.example.MainApplication</mainClass>
</manifest>
<manifestEntries>
<!--添加jar包同级目录下的其他目录作为到类路径 只能添加一个-->
<Class-Path>conf/ conf2/ conf3/</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
<!--maven-dependency-plugin打包依赖的插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!--包括或排除哪些scope的依赖-->
<includeScope>runtime</includeScope>
<excludeScope>provided,test</excludeScope>
<!--排除某些依赖-->
<excludeArtifactIds>junit,dbunit,mockito-all</excludeArtifactIds>
<!--是否排除传递依赖-->
<excludeTransitive>false</excludeTransitive>
<!--将依赖输出到这个目录下-->
<outputDirectory>
${project.build.directory}/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!--maven-assembly-plugin打包成指定格式的包 比如tar.gz-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<!-- <finalName>${project.artifactId}-${project.version}</finalName>-->
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
assembly.xml
点击查看代码
<?xml version="1.0" encoding="UTF-8"?>
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<id>build</id>
<formats>
<format>tar.gz</format>
</formats>
<!--是否将项目目录打入tar.gz-->
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<!--收集的文件集合-->
<fileSet>
<!--文件所在目录-->
<directory>target</directory>
<includes>
<include>*.jar</include>
<include>/lib/**</include>
<include>/conf/**</include>
</includes>
<!--在打包tar.gz的哪个目录-->
<outputDirectory>/</outputDirectory>
<directoryMode>0755</directoryMode>
</fileSet>
</fileSets>
</assembly>
maven-shade-plugin 打包可执行jar
- maven-shade-plugin
- 具体使用参数可以在pom里面点击对应参数,进入参数说明
点击查看代码
<!--打包为可执行的jar包 包括依赖-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<!--排除依赖 groupId:artifactId[[:type]:classifier]-->
<excludes>
<exclude>classworlds:classworlds</exclude>
<exclude>junit:junit</exclude>
<exclude>jmock:*</exclude>
<exclude>*:xml-apis</exclude>
<exclude>org.apache.maven:lib:tests</exclude>
</excludes>
</artifactSet>
<filters>
<!--使用filter更细粒度的控制-->
<filter>
<artifact>junit:junit</artifact>
<includes>
<include>junit/framework/**</include>
<include>org/junit/**</include>
</includes>
<excludes>
<exclude>org/junit/experimental/**</exclude>
<exclude>org/junit/runners/**</exclude>
</excludes>
</filter>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
<filter>
<artifact>foo:bar</artifact>
<!--默认为true 表示没有在includes里面的都会排除 设为false则不会排除-->
<excludeDefaults>false</excludeDefaults>
<includes>
<include>foo/Bar.class</include>
</includes>
</filter>
</filters>
<!--自动移除项目没有使用到的依赖-->
<!-- <minimizeJar>true</minimizeJar>-->
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>executable</shadedClassifierName>
<transformers>
<!--设置主类名-->
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.example.MainApplication</mainClass>
<!-- <Build-Number>123</Build-Number><!–自定义内容添加到manifest的–>-->
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
maven-shade-plugin打包springboot可执行jar
可参考spring-boot-starter-parent的pom,
点击查看代码
<!--打包可执行jar-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<configuration>
<keepDependenciesWithProvidedScope>true</keepDependenciesWithProvidedScope>
<createDependencyReducedPom>false</createDependencyReducedPom>
<!--下面两个配置合起来才有用-->
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>jar-with-dependencies</shadedClassifierName>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- <version>${spring.boot.version}</version>-->
<!-- <version>2.6.4</version>-->
<version>2.0.3.RELEASE</version><!--使用高版本报错-->
</dependency>
</dependencies>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer
implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
<resource>META-INF/spring.factories</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>${start-class}</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
maven编译proto配置
点击查看代码
<!--grpc版本号-->
<grpc.version>1.6.1</grpc.version>
<!--protobuf 版本号-->
<protobuf.version>3.9.0</protobuf.version>
<!--protobuf-->
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>${protobuf.version}</version>
</dependency>
<!--compile-->
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-core</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf-lite</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>${grpc.version}</version>
</dependency>
<build>
<plugins>
<!--proto编译-->
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.1</version>
<configuration>
<!--protobuf文件目录--> <protoSourceRoot>${project.basedir}/src/main/java/org/example/netty/protobuf/proto</protoSourceRoot>
<!--编译后文件的输出目录--> <outputDirectory>src/main/java/org/example/netty/protobuf/proto/entity</outputDirectory>
<!--设置是否在生成java文件之前清空outputDirectory的文件,默认值为true-->
<clearOutputDirectory>false</clearOutputDirectory>
<!--suppress UnresolvedMavenProperty -->
<protocArtifact>com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<!--使用来自该项目的一系列构建扩展 -->
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.5.0.Final</version>
</extension>
</extensions>
</build>
maven-antrun-plugin插件进行拷贝
点击查看代码
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>mvn-copy-file</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>copy-and-rename-file</id>
<phase>generate-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<copy file="${project.basedir}/xxx/xxx.txt" tofile="${project.build.directory}/abc.txt"/>
<move file="xxx" tofile="xxx"/>
</tasks>
</configuration>
</execution>
</executions>
<configuration>
<tasks>
<copy todir="${project.build.directory}/xxx/xxx">
<fileset dir="${project.build.directory}/" includes="*.tar.gz"/>
</copy>
</tasks>
</configuration>
</plugin>
build-helper-maven-plugin设置时间戳变量
设置的变量在文件中可以使用$进行引用,在使用maven-resources-plugin拷贝资源时filtering设置为true即可
点击查看代码
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>timestamp-property</id>
<goals>
<goal>timestamp-property</goal>
</goals>
<configuration>
<name>build.time</name>
<pattern>yyyy-MM-dd HH:mm:ss</pattern>
<locale>en_US</locale>
<timeZone>GMT+8</timeZone>
</configuration>
</execution>
</executions>
</plugin>
---
本文来自博客园,作者:Bingmous,转载请注明原文链接:https://www.cnblogs.com/bingmous/p/15643713.html