设为首页 加入收藏

TOP

Maven 构建配置文件(一)
2019-09-17 17:36:07 】 浏览:50
Tags:Maven 构建 配置 文件

什么是构建配置文件?

生成配置文件是一组可以用来设置或覆盖 Maven 构建配置值的默认值。使用生成配置文件,你可以针对不同的环境,如:生产V/S开发环境自定义构建。

配置文件中指定 pom.xml 文件使用其配置文件/配置文件元素和多种方式来触发。配置文件修改 POM 后,在编译的时候是用来给不同的目标环境参数(例如,开发,测试和生产环境的数据库服务器的路径)。

生成配置文件的类型

创建配置文件的文件主要有三种类型:

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

配置文件激活

Maven 构建配置文件的文件,可以使用以下几种方式来激活。

  • 明确使用命令从控制台输入。

  • 通过 Maven 设置。

  • 基于环境变量(用户/系统变量)。

  • OS设置(例如,Windows系列)。

  • 呈现/丢失的文件。

配置文件的文件激活的例子

我们假设你的项目如下的目录结构:

Maven Build Profile

现在下src/main/resources 有三个特定的文件:

文件名称 描述
env.properties 如果没有配置文件关联则使用默认配置
env.test.properties 当测试配置文件用于测试配置
env.prod.properties 生产配置时,prod信息被使用

显式配置文件激活

在下面的例子中,我们会附加上maven-antrun-plugin:run 插件:运行测试阶段目标。这将使我们能够为不同的配置文件调用文本消息。将在 pom.xml 中定义不同的配置文件,并在命令控制台使用 maven 命令将配置文件启动。

假设,我们建立如下的 pom.xml 在 C:\MVN\project 文件夹。 

<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>
   <groupId>com.companyname.projectgroup</groupId>
   <artifactId>project</artifactId>
   <version>1.0</version>
   <profiles>
      <profile>
      <id>test</id>
      <build>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.1</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"/>
                  </tasks>
                  </configuration>
               </execution>
            </executions>
         </plugin>
      </plugins>
      </build>
      </profile>
   </profiles>
   <dependencies>
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>3.8.1</version>
         <scope>test</scope>
      </dependency>
   </dependencies>
</project>

env.properties

environment=debug

env.test.properties

environment=test

env.prod.properties

environment=prod

现在,打开命令控制台,进入到包含 pom.xml 的文件夹并执行以下 mvn 命令。通过配置文件名作为参数可使用 -P 选项。

C:\MVN\project>mvn test -P test

Maven会开始处理并显示构建配置文件的测试结果。

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------
[INFO] Building Unnamed - com.companyname.projectgroup:project:jar:1.0
[INFO]    task-segment: [test]
[INFO] ------------------------------------------------------------------
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇项目架构开发:集群部署 下一篇企业管理系统前后端分离架构设计 ..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目