首页 > 其他分享 >Maven implicit Properties(隐含变量)

Maven implicit Properties(隐含变量)

时间:2023-07-31 10:33:13浏览次数:44  
标签:project reference implicit Maven POM Properties property properties


http://www.sonatype.com/books/mvnref-book/reference/resource-filtering-sect-properties.html


Maven: The Complete Reference 9.2. Maven Properties




9.2. Maven Properties



You can use Maven properties in a pom.xml file or in any resource that is being processed by the Maven Resource plugin’s filtering features. A property is always surrounded by ${ and }. For example, to reference the project.version property, one would write:



1.0



There are some implicit properties available in any Maven project, these implicit properties are:



project.* Maven Project Object Model (POM). You can use the project.* prefix to reference values in a Maven POM. settings.* Maven Settings. You use the settings.* prefix to reference values from your Maven Settings in ~/.m2/settings.xml. env.* Environment variables like PATH and M2_HOME can be referenced using the env.* prefix. System Properties Any property which can be retrieved from the System.getProperty() method can be referenced as a Maven property.



In addition to the implicit properties listed above, a Maven POM, Maven Settings, or a Maven Profile can define a set of arbitrary, user-defined properties. The following sections provide some detail on the various properties available in a Maven project.



9.2.1. Maven Project Properties



When a Maven Project Property is referenced, the property name is referencing a property of the Maven Project Object Model (POM). Specifically, you are referencing a property of the org.apache.maven.model.Model class which is being exposed as the implicit variable project. When you reference a property using this implicit variable, you are using simple dot notation to reference a bean property of the Model object. For example, when you reference ${project.version}, you are really invoking the getVersion() method on the instance of Model that is being exposed as project.

The POM is also represented in the pom.xml document present in all Maven projects. Anything in a Maven POM can be referenced with a property. A complete reference for the POM structure is available at http://maven.apache.org/ref/3.0.3/maven-model/maven.html. The following list shows some common property references from the Maven project.



project.groupId and project.version Projects in a large, multi-module build often share the same groupId and version identifiers. When you are declaring interdependencies between two modules which share the same groupId and version, it is a good idea to use a property reference for both:



<dependencies>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>sibling-project</artifactId>
        <version>${project.version}</version>
    </dependency>
</dependencies>



project.artifactId A project’s artifactId is often used as the name of a deliverable. For example, in a project with WAR packaging, you will want to generate a WAR file without the version identifiers. To do this, you would reference the project.artifactId in your POM file like this:


<build>
    <finalName>${project.artifactId}</finalName>
</build>


project.name and project.description The name and project description can often be useful properties to reference from documentation. Instead of having to worry that all of your site documents maintain the same short descriptions, you can just reference these properties. project.build.*

If you are ever trying to reference output directories in Maven, you should never use a literal value like target/classes. Instead you should use property references to refer to these directories.



  • project.build.sourceDirectory
  • project.build.scriptSourceDirectory
  • project.build.testSourceDirectory
  • project.build.outputDirectory
  • project.build.testOutputDirectory
  • project.build.directory



sourceDirectory, scriptSourceDirectory, and testSourceDirectory provide access to the source directories for the project. outputDirectory and testOutputDirectory provide access to the directories where Maven is going to put bytecode or other build output. directory refers to the directory which contains all of these output directories.



project.baseUri If you need a valid URI for your project’s base directory, you can use the ${project.baseUri} property. If your project is stored in the directory /tmp/simple, ${project.baseUri} will resolve to file:/private/tmp/simple/. Other Project Property references There are hundreds of properties to reference in a POM. A complete reference for the POM structure is available at http://maven.apache.org/ref/3.0.3/maven-model/maven.html.



For a full list of properties available on the Maven Model object, take a look at the JavaDoc for the maven-model project here http://maven.apache.org/ref/3.0.3/maven-model/apidocs/index.html. Once you load this JavaDoc, take a look at the Model class. From this Model class JavaDoc, you should be able to navigate to the POM property you wish to reference. If you needed to reference the output directory of the build, you can use the Maven Model JavaDoc to see that the output directory is referenced via model.getBuild().getOutputDirectory(); this method call would be translated to the Maven property reference ${project.build.outputDirectory}.

For more information about the Maven Model module, the module which defines the structure of the POM, see the Maven Model project page at http://maven.apache.org/ref/3.0.3/maven-model.



9.2.2. Maven Settings Properties



You can also reference any properties in the Maven Local Settings file which is usually stored in ~/.m2/settings.xml. This file contains user-specific configuration such as the location of the local repository and any servers, profiles, and mirrors configured by a specific user.

A full reference for the Local Settings file and corresponding properties is available here http://maven.apache.org/ref/3.0.3/maven-settings/settings.html.



9.2.3. Environment Variable Properties



Environment variables can be referenced with the env.* prefix. Some interesting environment variables are listed in the following list:



env.PATH Contains the current PATH in which Maven is running. The PATH contains a list of directories used to locate executable scripts and programs. env.HOME ${user.home} env.JAVA_HOME ${java.home} env.M2_HOME Contains the Maven 2 installation directory.



While they are available, you should always use the Java System properties if you have the choice. If you need a user’s home directory use ${user.home} instead of ${env.HOME}. If you do this, you’ll end up with a more portable build that is more likely to adhere to the Write-Once-Run-Anywhere (WORA) promise of the Java platform.



9.2.4. Java System Properties



Maven exposes all properties from java.lang.System. Anything you can retrieve from System.getProperty() you can reference in a Maven property. The following table lists available properties:



Table 9.1. Java System Properties



System Property

Description

java.version

Java Runtime Environment version

java.vendor

Java Runtime Environment vendor

java.vendor.url

Java vendor URL

java.home

Java installation directory

java.vm.specification.version

Java Virtual Machine specification version

java.vm.specification.vendor

Java Virtual Machine specification vendor

java.vm.specification.name

Java Virtual Machine specification name

java.vm.version

Java Virtual Machine implementation version

java.vm.vendor

Java Virtual Machine implementation vendor

java.vm.name

Java Virtual Machine implementation name

java.specification.version

Java Runtime Environment specification version

java.specification.vendor

Java Runtime Environment specification vendor

java.specification.name

Java Runtime Environment specification name

java.class.version

Java class format version number

java.class.path

Java class path

java.ext.dirs

Path of extension directory or directories

os.name

Operating system name

os.arch

Operating system architecture

os.version

Operating system version

file.separator

File separator ("/" on UNIX, "\" on Windows)

path.separator

Path separator (":" on UNIX, ";" on Windows)

line.separator

Line separator ("\n" on UNIX and Windows)

user.name

User’s account name

user.home

User’s home directory

user.dir

User’s current working





9.2.5. User-defined Properties



In addition to the implicit properties provided by the POM, Maven Settings, environment variables, and the Java System properties, you have the ability to define your own arbitrary properties. Properties can be defined in a POM or in a Profile. The properties set in a POM or in a Maven Profile can be referenced just like any other property available throughout Maven. User-defined properties can be referenced in a POM, or they can be used to filter resources via the Maven Resource plugin. Here’s an example of defining some arbitrary properties in a Maven POM.

User-defined Properties in a POM. 



<project>
    ...
    <properties>
        <arbitrary.property.a>This is some text</arbitrary.property.a>
        <hibernate.version>3.3.0.ga</hibernate.version>
    </properties>
    ...
    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
    </dependencies>
    ...
</project>



The previous example defines two properties: arbitrary.property.a and hibernate.version. The hibernate.version is referenced in a dependency declaration. Using the period character as a separator in property names is a standard practice throughout Maven POMs and Profiles. The next example shows you how to define a property in a profile from a Maven POM.

User-defined Properties in a Profile in a POM. 


<project>
    ...
    <profiles>
        <profile>
            <id>some-profile</id>
            <properties>
                <arbitrary.property>This is some text</arbitrary.property>
            </properties>
        </profile>
    </profiles>
    ...
</project>



The previous example demonstrates the process of defining a user-defined property in a profile from a Maven POM. For more information about user-defined properties and profiles, see Chapter 5, Build Profiles.


 

标签:project,reference,implicit,Maven,POM,Properties,property,properties
From: https://blog.51cto.com/u_16174476/6905887

相关文章

  • 使用Eclipse构建Maven的SpringMVC项目
    使用Eclipse构建Maven的SpringMVC项目      首先Eclipse需要安装Maven的插件,地址:http://m2eclipse.sonatype.org/sites/m2e。     用MyEclipse安装Maven插件,建出的Maven项目有些问题。一是,发布tomcat的时候resources总是不会被发布到tomcat下;二是,把WEB-INF下的cla......
  • 无涯教程-jQuery - css( properties )方法函数
    css(properties)方法将键/值对象设置为所有匹配元素的样式属性。css(properties)-语法selector.css(properties)上面的语法可以写成如下-selector.css({key1:val1,key2:val2....keyN:valN})这是此方法使用的所有参数的描述-key:value   - 设置为样式属性......
  • mac安装maven环境并配置
    参考:https://www.bilibili.com/video/BV1mW4y1q72A/?spm_id_from=333.337.search-card.all.click&vd_source=56102d8741b60f8a5fd28fed8a3d6e46https://blog.csdn.net/hzqit520/article/details/129166916......
  • maven前言
    mvn大家都在用,但是确实是不求甚解。在真实项目中,大家只需要copy一个pom文件,修修补补,记录几个常用命令就OK了。这叫面向切面编程,专注自己的核心逻辑,但是mvn本身确实是非常复杂的工具。兼顾了CICD整个寿命周期,为此实现的插件编码总量几乎大于任何一个服务实例的开发量。往往对于......
  • 将resources和testresources交给maven管理
    就两个标签和分别管理对应模块配置文件和测试配置文件,举例说明<build><resources><resource><directory>${project.basedir}/src/main/resources</directory><filtering>true</filtering></resource></resources&g......
  • maven异常-Cannot resolve com.sun.jmx:jmxri:1.2.1
    mavenCannotresolvecom.sun.jmx:jmxri:1.2.1定位到maven依赖报错的jarkafka.jar里的jmxri报错,排除jmxri即可 <dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka_2.9.2</artifactId> <version>0.8.1</version> ......
  • 一文带你搞定Maven全功能
    在一次需求迭代中,同事要求我把写好的RPC接口打好包上传到公司私服上,我人直接当场懵逼住了。突然发现自己对于Maven仅仅是处于最基础的使用阶段,不仅不知道背后的一些原理,甚至连一些常见的概念都不是很清晰,仅仅会使用Maven构建项目,引入依赖,打包等最基础的操作,所以连忙补补课,成功完成......
  • idea maven 命令后控制台乱码
    首先在idea中查看maven的编码方式mvn-vMavenhome:D:\apache-maven-3.6.3\bin\..Javaversion:11.0.17,vendor:OracleCorporation,runtime:C:\ProgramFiles\Java\jdk-11.0.17Defaultlocale:zh_CN,platformencoding:GBKOSname:"windows11",version:......
  • maven的 ${project.basedir}
    <dependency><groupId>com.utils.module</groupId><artifactId>dsg-public-operation</artifactId><version>1.0.1</version><scope>system</scope><systemPath>${project.basedir}/lib/com......
  • spring-boot 打包 配置、lib、脚本分开 的maven配置
    <profiles><profile><id>dev</id><properties><spring.profiles.active>dev</spring.profiles.active></properties></profile>......