Maven的构建配置文件(Build Profiles)

Maven的构建配置文件(Build Profiles)

在命令行使用构建配置文件时,是-P,比如:mvn -Pinput

注意:这里的构建配置文件并不是一个真正的文件,而是通过指定参数来做特定的事。

以下内容引用自https://ayayui.gitbooks.io/tutorialspoint-maven/content/book/maven_build_profiles.html

当时此教程的例子是在2.0版本,而新的3.0版本只是增加了一点,具体可以参考官网http://maven.apache.org/guides/introduction/introduction-to-profiles.html

什么是构建配置文件?

构建配置文件(A Build profile) 是一系列的配置项的值,可以用来设置或者覆盖Maven构建默认值。使用构建配置文件,你可以为不同的环境,比如说生产环境(Producation)和开发(Development)环境,定制构建方式。

配置文件在pom.xml文件中使用activeProfiles或者profiles元素指定,并且可以通过各种方式触发。配置文件在构建时修改POM,并且用来给参数设定不同的目标环境(比如说,开发(Development)、测试(Testing)和生产环境(Producation)中数据库服务器的地址)。

构建配置文件的类型

构建配置文件大体上有三种类型

类型在哪定义
项目级(Per Project)定义在项目的POM文件pom.xml中
用户级 (Per User)定义在Maven的设置xml文件中 (%USER_HOME%/.m2/settings.xml)
全局(Global)定义在Maven全局的设置xml文件中 (%M2_HOME%/conf/settings.xml)

配置文件激活

Maven的构建配置文件可以通过多种方式激活。

  • 使用命令控制台输入显式激活。
  • 通过maven设置。
  • 基于环境变量(用户或者系统变量)。
  • 操作系统设置(比如说,Windows系列)。
  • 文件的存在或者缺失。

官方配置文件激活示例

http://maven.apache.org/guides/introduction/introduction-to-profiles.html

http://maven.apache.org/ref/2.2.1/maven-profile/profiles.html

实践配置文件激活示例

新建的项目结构如下:

其中在src/main/resources文件夹下有三个用于测试文件:

文件名描述
env.properties如果未指定配置文件时默认使用的配置。
env.test.properties当测试配置文件使用时的测试配置。
env.prod.properties当生产配置文件使用时的生产配置。

注意:这三个配置文件并不是代表构建配置文件的功能,而是用于本次测试的目的;比如,我指定了构建配置文件为prod时,项目就使用envprod.properties文件。

注意:下面的例子仍然是使用AntRun插件,因为此插件能绑定Maven生命周期阶段,并通过Ant的标签不用编写一点代码即可输出信息、复制文件等,经此而已。其余的与本次构建配置文件无关。

1、显示配置文件激活

pom.xml配置如下:

复制代码

<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>com.jsoft.test</groupId>
  <artifactId>testproject</artifactId>
  <packaging>jar</packaging>
  <version>0.1-SNAPSHOT</version>
  <name>testproject</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <profiles>
      <profile>
          <id>test</id>
          <build>
              <plugins>
                 <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.8</version>
                    <executions>
                       <execution>
                          <phase>test</phase>
                          <goals>
                             <goal>run</goal>
                          </goals>
                          <configuration>
                          <tasks>
                             <echo>Using env.test.properties</echo>
                             <copy file="src/main/resources/env.test.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/>
                          </tasks>
                          </configuration>
                       </execution>
                    </executions>
                 </plugin>
              </plugins>
          </build>
      </profile>
      <profile>
          <id>normal</id>
          <build>
              <plugins>
                 <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.8</version>
                    <executions>
                       <execution>
                          <phase>test</phase>
                          <goals>
                             <goal>run</goal>
                          </goals>
                          <configuration>
                          <tasks>
                             <echo>Using env.properties</echo>
                             <copy file="src/main/resources/env.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/>
                          </tasks>
                          </configuration>
                       </execution>
                    </executions>
                 </plugin>
              </plugins>
          </build>
      </profile>
      <profile>
          <id>prod</id>
          <build>
              <plugins>
                 <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.8</version>
                    <executions>
                       <execution>
                          <phase>test</phase>
                          <goals>
                             <goal>run</goal>
                          </goals>
                          <configuration>
                          <tasks>
                             <echo>Using env.prod.properties</echo>
                             <copy file="src/main/resources/env.prod.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/>
                          </tasks>
                          </configuration>
                       </execution>
                    </executions>
                 </plugin>
              </plugins>
          </build>
      </profile>
   </profiles>
</project>

复制代码

注意:构建配置文件采用的是<profiles>节点。

说明:上面新建了三个<profiles>,其中<id>区分了不同的<profiles>执行不同的AntRun任务;而AntRun的任务可以这么理解,AntRun监听test的Maven生命周期阶段,当Maven执行test时,就除了发AntRun的任务,任务里面为输出文本并复制文件到指定的位置;而至于要执行哪个AntRun任务,此时构建配置文件起到了传输指定的作用,比如,通过命令行参数输入指定的<id>

执行命令:

mvn test -Ptest

提示:第一个test为Maven生命周期阶段,第2个test为为构建配置文件指定的<id>参数,这个参数通过-P来传输,当然,它可以是prod或者normal这些由你定义的<id>

运行的结果如下:

可以看出成功的触发了AntRun的任务。并且是对应构建配置文件下的<id>为test的任务。

再测试其余两个命令,结果如下:

2、通过Maven设置激活配置文件

打开%USER_HOME%/.m2目录下的settings.xml文件,其中%USER_HOME%代表用户主目录。如果setting.xml文件不存在就直接拷贝%M2_HOME%/conf/settings.xml到.m2目录,其中%M2_HOME%代表Maven的安装目录。对于为什么可以这样做,参考:http://www.cnblogs.com/EasonJim/p/6827058.html

配置setting.xml文件,增加<activeProfiles>属性:

复制代码

<settings 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/settings-1.0.0.xsd">
   ...
   <activeProfiles>
      <activeProfile>test</activeProfile>
   </activeProfiles>
</settings>

复制代码

执行命令:

mvn test

提示:此时不需要使用-Ptest来输入参数了,上面的setting.xml文件的<activeprofile>已经指定了test参数,代替了。

提示2:同样可以使用在%M2_HOME%/conf/settings.xml的文件进行配置,效果一致。

执行结果:

3、通过环境变量激活配置文件

先把上一步测试的setting.xml值全部去掉。

然后在pom.xml里面的<id>为test的<profile>节点,加入<activation>节点:

复制代码

<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>com.jsoft.test</groupId>
  <artifactId>testproject</artifactId>
  <packaging>jar</packaging>
  <version>0.1-SNAPSHOT</version>
  <name>testproject</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <profiles>
      <profile>
          <id>test</id>
          <activation>
            <property>
               <name>env</name>
               <value>test</value>
            </property>
          </activation>
          <build>
              <plugins>
                 <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.8</version>
                    <executions>
                       <execution>
                          <phase>test</phase>
                          <goals>
                             <goal>run</goal>
                          </goals>
                          <configuration>
                          <tasks>
                             <echo>Using env.test.properties</echo>
                             <copy file="src/main/resources/env.test.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/>
                          </tasks>
                          </configuration>
                       </execution>
                    </executions>
                 </plugin>
              </plugins>
          </build>
      </profile>
      <profile>
          <id>normal</id>
          <build>
              <plugins>
                 <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.8</version>
                    <executions>
                       <execution>
                          <phase>test</phase>
                          <goals>
                             <goal>run</goal>
                          </goals>
                          <configuration>
                          <tasks>
                             <echo>Using env.properties</echo>
                             <copy file="src/main/resources/env.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/>
                          </tasks>
                          </configuration>
                       </execution>
                    </executions>
                 </plugin>
              </plugins>
          </build>
      </profile>
      <profile>
          <id>prod</id>
          <build>
              <plugins>
                 <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.8</version>
                    <executions>
                       <execution>
                          <phase>test</phase>
                          <goals>
                             <goal>run</goal>
                          </goals>
                          <configuration>
                          <tasks>
                             <echo>Using env.prod.properties</echo>
                             <copy file="src/main/resources/env.prod.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/>
                          </tasks>
                          </configuration>
                       </execution>
                    </executions>
                 </plugin>
              </plugins>
          </build>
      </profile>
   </profiles>
</project>

复制代码

执行命令:

mvn test -Denv=test

提示:上面使用-D传递环境变量,其中evn对应刚才设置的<name>值,test对应<value>。

提示2:在Windows 10上测试了系统的环境变量,但是不生效,所以,只能通过-D传递。

执行结果:

4、通过操作系统激活配置文件

5、通过文件的存在或者缺失激活配置文件

6、通过JDK的版本激活配置文件

...

更多激活配置,可以参考官方的例子:http://maven.apache.org/guides/introduction/introduction-to-profiles.html

 

测试工程:https://github.com/easonjim/5_java_example/tree/master/maventest/test4/test4/testproject

分类: 编程语言-[Java]

标签: javamaventutorialspoint-maven实践笔记

好文要顶 关注我 收藏该文  

2

« 上一篇: Jenkins构建完成后通过SVN Publisher Plugin上传文件到指定的SVN(教程收集) 
» 下一篇: Maven中的-D(Properties属性)和-P(Profiles配置文件)

posted @ 2017-05-09 04:29  EasonJim  阅读(13937)  评论(0)  编辑  收藏  举报

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 阿里云 Maven配置文件主要包含以下几个部分: 1. settings.xml 文件:该文件是 Maven 的全局配置文件,一般位于 Maven 的安装目录下的 conf 文件夹中。其中配置了镜像、代理、认证等信息。以下是示例的 settings.xml 配置内容: ```xml <settings> <mirrors> <mirror> <id>aliyun</id> <name>aliyun maven mirror</name> <url>http://maven.aliyun.com/nexus/content/repositories/central/</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors> <profiles> <profile> <id>aliyun</id> <activation> <activeByDefault>true</activeByDefault> </activation> <repositories> <repository> <id>aliyun</id> <name>aliyun maven repository</name> <url>http://maven.aliyun.com/nexus/content/repositories/central/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </profile> </profiles> <servers> <server> <id>aliyun</id> <username>your-username</username> <password>your-password</password> </server> </servers> </settings> ``` 2. pom.xml 文件:该文件是具体 Maven 项目的配置文件,位于项目的根目录下。其中配置了项目的依赖、插件等信息。以下是示例的 pom.xml 配置内容: ```xml <project> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>my-project</artifactId> <version>1.0.0</version> <repositories> <repository> <id>aliyun</id> <name>aliyun maven repository</name> <url>http://maven.aliyun.com/nexus/content/repositories/central/</url> </repository> </repositories> <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>my-library</artifactId> <version>1.0.0</version> </dependency> </dependencies> </project> ``` 以上就是阿里云 Maven配置文件完整内容,通过配置这些文件,可以实现使用阿里云的镜像来加速 Maven 项目的构建和依赖下载。 ### 回答2: 阿里云Maven配置文件包括两部分:全局配置文件settings.xml和项目配置文件pom.xml。 全局配置文件settings.xml用于配置Maven的全局设置,路径在Maven安装目录下的conf文件夹下。以下是阿里云Maven配置文件完整内容示例: ```xml <?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd"> <localRepository>${user.home}/.m2/repository</localRepository> <mirrors> <mirror> <id>aliyunmaven</id> <name>阿里云公共仓库</name> <url>https://maven.aliyun.com/repository/public</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors> <profiles> <profile> <id>default</id> <repositories> <repository> <id>aliyunmaven</id> <name>阿里云公共仓库</name> <url>https://maven.aliyun.com/repository/public</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>aliyunmaven</id> <name>阿里云公共仓库</name> <url>https://maven.aliyun.com/repository/public</url> </pluginRepository> </pluginRepositories> </profile> </profiles> <activeProfiles> <activeProfile>default</activeProfile> </activeProfiles> </settings> ``` 项目配置文件pom.xml用于配置具体项目的Maven依赖和构建配置。以下是一个简单的pom.xml配置文件示例: ```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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>my-project</artifactId> <version>1.0.0</version> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.5.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.5.2</version> </plugin> </plugins> </build> </project> ``` 需要注意的是,具体的配置内容根据项目需求和实际情况可以进行调整和扩展。以上是阿里云Maven配置文件的一种完整示例。 ### 回答3: 阿里云 Maven配置文件完整中,主要涉及到两个文件:settings.xml 和 pom.xml。 1. settings.xml 文件配置: 这个文件是全局的 Maven 配置文件,位于 Maven 安装目录的 conf 文件夹下,或者是用户目录的 .m2 文件夹下。配置内容如下: ```xml <settings> ... <mirrors> <mirror> <id>alimaven</id> <url>https://maven.aliyun.com/repository/public</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors> ... <profiles> <profile> <id>alimaven</id> <repositories> <repository> <id>aliyun</id> <name>aliyun Maven</name> <url>https://maven.aliyun.com/repository/public</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </profile> </profiles> ... <activeProfiles> <activeProfile>alimaven</activeProfile> </activeProfiles> ... </settings> ``` 上述配置中,``<mirror>``元素指定了镜像仓库,将``<url>``值设置为阿里云的 Maven 仓库地址。``<profile>``元素定义了一个名为 alimaven配置文件,其中包含了仓库的相关信息。同时,通过设置``<activeProfiles>``将 alimaven 作为活跃配置。 2. pom.xml 文件配置: pom.xml 是 Maven 项目的配置文件,位于项目根目录下。在其中添加如下依赖配置: ```xml <repositories> <repository> <id>aliyun</id> <name>aliyun Maven</name> <url>https://maven.aliyun.com/repository/public</url> </repository> </repositories> ``` 上述配置将阿里云 Maven 仓库添加到项目中,使得项目可以从阿里云 Maven 仓库下载依赖库。 以上就是阿里云 Maven 配置文件的完整内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值