背景
Java項目牲蜀,想要在生成的jar名稱中蔼两,帶上部署環(huán)境的名稱酝枢,如:xxx-${env}.jar
于是開始查資料,發(fā)現(xiàn)需要好幾個概念瓢剿,如Interpolation
, Variables
, Project Model Variables
, Special Variables
, Properties
, Profile
; 也經(jīng)歷里多次搜索和資料消化(資料附在每節(jié)內(nèi)容之后)逢慌,才拼湊出一個可行的方案。官網(wǎng)文檔是不夠完善的间狂,多走了些彎路攻泼。
故有此文。
一. 如何自定義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>
二. 變量部分怎么處理
POM中有哪些可以引用的變量形式呢鉴象?
1. 在名稱中使用變量
官網(wǎng)文檔中提到忙菠,變量有三種:
- Project Model Variables
{project.version}, ${project.build.sourceDirectory}
- Special Variables
project.basedir, project.baseUri, maven.build.timestamp - 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>
2. 第四種變量 - 環(huán)境變量
傳入:
$ 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>
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小節(jié)中,Properties
是作為Variables
中的一種被提到的纺弊,但在這份資料中(源自codehaus.org)牛欢,Properties變成了統(tǒng)稱,既覆蓋了Variables
的范圍, 也包含了環(huán)境變量(第2小節(jié))俭尖,Java系統(tǒng)屬性氢惋,還完美地對第3小節(jié)提到的用法,給了基礎有力的歸類:自定義的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`
另外發(fā)現(xiàn)一本Maven免費電子書浆兰。以后再找Maven資料,可以先在這里看看基礎珊豹,搞清概念簸呈。??
[Maven: The Complete Reference](Maven: The Complete Reference)
其中與今日話題相關的內(nèi)容:
9.2. Maven Properties