Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具。
1. 安装maven
linux下安装
下载apache-maven-3.5.4-bin.tar.gz
cp -r /home/wqf/download/apache-maven-3.5.4 /home/wqf/usr/lib
vim ~/.bash_profile
export MAVEN_HOME=/home/wqf/usr/lib/apache-maven-3.5.4
export PATH=$PATH:$MAVEN_HOME/bin
source ~/.bash_profile命令使改动生效。
验证安装是否成功:
modelVersion --这个值通常被设置为4.0.0,goroupId--表示项目组Id,artifactId--项目Id,version--项目版本号
maven中的插件:build plugins,在构建项目的时候执行,被配置在<build><plugins><plugins><build>元素当中
maven中的profile:profile可以让我们定义一系列的配置信息,然后指定其激活条件。这样我们就可以定义多个profile,然后每个profile对应不同的激活条件和配置信息,从而达到不同环境使用不同配置信息的效果。
profile定义的位置:
(1)针对于特定项目的profile配置我们可以定义在该项目的pom.xml中。(下面举例是这种方式)
(2)针对于特定用户的profile配置,我们可以在用户的settings.xml文件中定义profile。该文件在用户家目录下的“.m2”目录下。
(3)全局的profile配置。全局的profile是定义在Maven安装目录下的“conf/settings.xml”文件中的。
使用maven打包时可选择的途径很多,可以使用build/resouces来打包,可以使用maven-assembly-plugin来打包,还有一些其他的plugin也可以用。
比如通过build/resources可以写成这样:
<build>
<finalName>sh-decoder-config</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
<filters>
<filter>src/environments/${env}.properties</filter>
</filters>
<resources>
<resource>
<directory>src/resources/cronjobs</directory>
<filtering>true</filtering>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
</build>
使用maven-assembly-plugin可以写成这样:
<build>
<finalName>${project.artifactId}_${maven.build.timestamp}_1</finalName>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions> <!--执行器 mvn assembly:assembly-->
<execution>
<id>make-package</id><!--名字任意 -->
<phase>package</phase><!-- 绑定到package生命周期阶段上 -->
<goals>
<goal>single</goal><!-- 只运行一次 -->
</goals>
<configuration>
<descriptors> <!--描述文件路径 配置多个 就一起打出包了-->
<descriptor>src/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<filters>
<filter>src/environments/${env}.properties</filter>
</filters>
</build>
下面以自己的项目为例,配置文件源目录结构如下:
src同一级有pom.xml和shell搅拌package.sh
.
└── src
├── assembly
├── environments
└── resources
├── cronjobs
├── dispatcher
│ ├── bin
│ └── config
├── redis
│ └── config
├── sh_decoder
│ └── config
└── websocket
├── bin
└── config
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>AMD</groupId>
<artifactId>yunmd</artifactId>
<version>1.0.0</version>
<repositories>
<repository>
<id>maven-net</id>
<name>Maven Mirror</name>
<url>http://ip:port/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>maven-net</id>
<name>Maven Mirror</name>
<url>http://ip:port/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<properties>
<maven.build.timestamp.format>yyyyMMdd</maven.build.timestamp.format>
<sh_decoder_binary_path>/home/mdman/dist/sh_marketdata/20180927_1/prod1/sh_decoder/bin/sh_decoder</sh_decoder_binary_path>
</properties>
<build>
<finalName>${project.artifactId}_${maven.build.timestamp}_1</finalName>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions> <!--执行器 mvn assembly:assembly-->
<execution>
<id>make-package</id><!--名字任意 -->
<phase>package</phase><!-- 绑定到package生命周期阶段上 -->
<goals>
<goal>single</goal><!-- 只运行一次 -->
</goals>
<configuration>
<descriptors> <!--描述文件路径 配置多个 就一起打出包了-->
<descriptor>src/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<filters>
<filter>src/environments/${env}.properties</filter>
</filters>
</build>
<profiles>
<profile>
<id>qa1</id>
<properties>
<env>qa1</env>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>prod1</id>
<properties>
<env>prod1</env>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
</project>
package.sh
mvn clean package -P prod1
mvn package -P qa1
pack_path=`find . -maxdepth 3 -mindepth 3 -name yunmd\* -type d`
pack_name=${pack_path##*/}
cp -r $pack_path .
zip -r $pack_name".zip" $pack_name