Maven pom.xml中的变量

背景

Java项目,想要在生成的jar名称中,带上部署环境的名称,如:xxx-${env}.jar
于是开始查资料,发现需要好几个概念,如Interpolation, Variables, Project Model Variables, Special Variables, Properties, Profile; 也经历里多次搜索和资料消化(资料附在每节内容之后),才拼凑出一个可行的方案。官网文档是不够完善的,多走了些弯路。

故有此文。

一. 如何自定义Maven 生成的jar的名称

设置 finalName

    <build>
        <finalName>eureka-server-1.2</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

(19条消息) maven指定打的jar包的名字_tinysakura的博客-CSDN博客

二. 变量部分怎么处理

POM中有哪些可以引用的变量形式呢?

1. 在名称中使用变量

官网文档中提到,变量有三种:

  1. Project Model Variables
    {project.groupId},{project.version}, ${project.build.sourceDirectory}
  2. Special Variables
    project.basedir, project.baseUri, maven.build.timestamp
  3. Properties
<project>
  ...
  <properties>
    <mavenVersion>3.0</mavenVersion>
  </properties>
 
  <dependencies>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-artifact</artifactId>
      <version>${mavenVersion}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-core</artifactId>
      <version>${mavenVersion}</version>
    </dependency>
  </dependencies>
  ...
</project>

Project Interpolation and Variables

2. 第四种变量 - 环境变量

传入:

$ export JAVA_VERSION=9
$ mvn clean package

使用:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>${env.JAVA_VERSION}</source>
                <target>${env.JAVA_VERSION}</target>
            </configuration>
        </plugin>
    </plugins>
</build>

Refer to Environment Variables in pom.xml | Baeldung

3. 第五种变量 - profile

声明一组profiles:

    <profiles>  
        <profile>  
            <id>dev</id>  
            <properties>  
                <env>dev</env>  
            </properties>  
            <activation>  
                <activeByDefault>true</activeByDefault>  
            </activation>  
        </profile> 
        <profile>  
            <id>qa</id>  
            <properties>  
                <env>qa</env>  
            </properties>  
        </profile>  
        <profile>  
            <id>pre</id>  
            <properties>  
                <env>pre</env>  
            </properties>  
        </profile>  
        <profile>  
            <id>prod</id>  
            <properties>  
                <env>prod</env>  
            </properties>  
        </profile>  
    </profiles>  

传入:

$  mvn clean package -Dmaven.test.skip=true -P product

使用:
${project.activeProfiles[0].id}

java - Maven - Can I reference profile id in profile definition? - Stack Overflow

6. 第X种变量 & 另眼相看 Properties

第1小节中,Properties是作为Variables中的一种被提到的,但在这份资料中(源自codehaus.org),Properties变成了统称,既覆盖了Variables的范围, 也包含了环境变量(第2小节),Java系统属性,还完美地对第3小节提到的用法,给了基础有力的归类:自定义的Properties。

文中有这么一句话,All elements in the pom.xml, can be referenced with the project. prefix.。我认为这是非常重要的基础概念,遗憾的是没有在前面的资料中看到过。所以,一切在POM中可以引用的变量,也都可以叫做Properties。概念上模糊的地方就在于此了。这里“把概念弄清”的含义,就变成为:概念定义原本就是那么模糊,但是从此知道它们有时可以指代对方😂。

今日份最重要参考资料:

predefined_maven_properties/README.md at master · cko/predefined_maven_properties`

另外发现一本Maven免费电子书。以后再找Maven资料,可以先在这里看看基础,搞清概念。😄

[Maven: The Complete Reference](Maven: The Complete Reference)
其中与今日话题相关的内容:
9.2. Maven Properties

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容