标签:sch war包 environ type 设置 repos 存在 通过 man
由一次打包部署失败引发的深入探索┑( ̄▽  ̄)┍
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- The Basics -->
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<packaging>...</packaging>
<dependencies>...</dependencies>
<parent>...</parent>
<dependencyManagement>...</dependencyManagement>
<modules>...</modules>
<properties>...</properties>
<!-- Build Settings -->
<build>...</build>
<reporting>...</reporting>
<!-- More Project Information -->
<name>...</name>
<description>...</description>
<url>...</url>
<inceptionYear>...</inceptionYear>
<licenses>...</licenses>
<organization>...</organization>
<developers>...</developers>
<contributors>...</contributors>
<!-- Environment Settings -->
<issueManagement>...</issueManagement>
<ciManagement>...</ciManagement>
<mailingLists>...</mailingLists>
<scm>...</scm>
<prerequisites>...</prerequisites>
<repositories>...</repositories>
<pluginRepositories>...</pluginRepositories>
<distributionManagement>...</distributionManagement>
<profiles>...</profiles>
</project>
groupId+artifactId+version:组成项目的唯一定位,当groupId和version是从父级继承时,则不必显式定义他们;
packaging:定义打包方式,当前主要的打包方式有pom、 jar、 maven-plugin、 ejb、 war、 ear、 rar,其中jar为默认打包方式;
properties:定义pom常量,pom常量可以在pom文件的任意地方通过${}来引用;
构建配置分为两种,"Project Build"和"Profile Build"
<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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<!-- "Project Build" contains more elements than just the BaseBuild set -->
<build>...</build>
<profiles>
<profile>
<!-- "Profile Build" contains a subset of "Project Build"s elements -->
<build>...</build>
</profile>
</profiles>
</project>
build:定义项目的目录结构和插件管理;
<build>
<defaultGoal>install</defaultGoal>
<directory>${basedir}/target</directory>
<finalName>${artifactId}-${version}</finalName>
<filters>
<filter>filters/filter1.properties</filter>
</filters>
...
</build>
defaultGoal:指定默认的目标或者阶段,比如目标可以是jar:jar,阶段可以是install,这两个效果相同;
directory:构建的目标目录,默认是${basedir}/target;
finalName:项目最终生成的名字,默认是${artifactId}-${version};
filters:定义需要应用的*.properties文件。换句话说就是,在构建时筛选器文件中定义的“name=value”会替换掉资源文件中${name}字符串。Maven的默认筛选器目录是${basedir}/src/main/filters;
resources:资源列表,描述与项目关联的文件的内容和位置
<build>
...
<resources>
<resource>
<targetPath>META-INF/plexus</targetPath>
<filtering>false</filtering>
<directory>${basedir}/src/main/plexus</directory>
<includes>
<include>configuration.xml</include>
</includes>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
</resource>
</resources>
...
</build>
targetPath:定义项目生成后资源文件放置的位置,默认位置是${basedir};
filtering:可选值为true或false,定义是否为此资源启用筛选。与filters、profile元素结合使用;
directory:定义在哪找到资源文件,默认位置是${basedir}/src/main/resources;
includes:定义directory下需要包含的资源文件,支持*作为通配符匹配文件名;
excludes:结构同includes,定义directory下需要忽略的资源文件,当includes和excludes有冲突时,以excludes为准;
profiles:根据不同的构建环境更改设置;
<profiles>
<profile>
<id>test</id>
<activation>...</activation>
<build>...</build>
<modules>...</modules>
<repositories>...</repositories>
<pluginRepositories>...</pluginRepositories>
<dependencies>...</dependencies>
<reporting>...</reporting>
<dependencyManagement>...</dependencyManagement>
<distributionManagement>...</distributionManagement>
</profile>
</profiles>
activation:描述当前profile激活的条件,当activation里面描述的条件都满足时,就会激活当前profile;
<profiles>
<profile>
<id>test</id>
<activation>
<activeByDefault>false</activeByDefault>
<jdk>1.5</jdk>
<os>
<name>Windows XP</name>
<family>Windows</family>
<arch>x86</arch>
<version>5.1.2600</version>
</os>
<property>
<name>sparrow-type</name>
<value>African</value>
</property>
<file>
<exists>${basedir}/file2.properties</exists>
<missing>${basedir}/file1.properties</missing>
</file>
</activation>
...
</profile>
</profiles>
activeByDefault:是否是默认激活;
jdk:运行环境的jdk版本号;
os:运行环境的操作系统的属性;
file:哪些文件存在或者哪些文件缺失;
Maven项目有时候会遇到有第三方依赖打包的情况,如果这个时候不想deploy到Maven库上,可以这样做:
打jar包
1、新建一个目录用于存放第三方依赖,目录位置看个人喜好,这里新建lib放在项目根目录下;
2、pom文件中添加依赖;
<dependency>
<groupId>xx</groupId>
<artifactId>xx</artifactId>
<version>2.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/xx-xx-2.0.jar</systemPath>
</dependency>
3、在pom文件的build元素下添加resource配置,目的是将lib里的第三方依赖打到BOOT-INF/lib/下。这里还需要再配置另一个resource,因为添加前面这个配置后会覆盖掉默认的resource配置导致项目原本的资源文件没有打进构建生成的包里;
<resources>
<resource>
<directory>lib</directory>
<targetPath>BOOT-INF/lib/</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
打war包
1、2步骤同jar包,3步骤需要这样修改
<!--设置maven-war-plugins插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resource>
<directory>lib</directory>
<targetPath>WEB-INF/lib/</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
1、新建一个目录用于存放filter配置文件,这里新建filters目录,并存在localhost.properties、test.properties、prod.properties三个环境的配置文件。
2、pom文件中添加profiles配置
<profiles>
<profile>
<id>localhost</id>
<properties>
<profiles.active>localhost</profiles.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<profiles.active>test</profiles.active>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
<profile>
<id>prod</id>
<properties>
<profiles.active>prod</profiles.active>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
</profiles>
3、pom文件中的build元素下添加配置
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<filters>
<filter>${basedir}/filters/${profiles.active}.properties</filter>
</filters>
4、${basedir}/src/main/resources目录下编写总配置文件,使用${name}占位符(SpringBoot项目使用@name@占位符)来获取filters中配置文件对应环境的配置值
参考文章:
https://www.jianshu.com/p/574f74d1d0ee
http://maven.apache.org/pom.html
Maven配置、第三方依赖jar包打包以及Profiles多环境配置
标签:sch war包 environ type 设置 repos 存在 通过 man
原文地址:https://www.cnblogs.com/irain/p/12030646.html