• 21)pom 中的缺省值(default properties)


    1 引言

    项目中build 时用到了maven-jar-plugin ,其中有一个 ${project.build.directory}

                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.0.2</version>
                    <configuration>
                        <outputDirectory>${project.build.directory}/pdt/lib</outputDirectory>
                    </configuration>
                </plugin>

    正常理解是:在这个pom中或者父pom中通过 <properties> 定义这个属性

    但是找了许久没有找到,通过查找各种资料才知道这是一个缺省值(dafault-value)

    是项目的 target/

    理所当然便引出了"究竟有多少缺省值?" 这个问题

    2 如何获取属性

    http://maven.apache.org/guides/getting-started/

    Sometimes a resource file will need to contain a value that can only be supplied at build time. To accomplish this in Maven, put a reference to the property that will contain the value into your resource file using the syntax ${<property name>}. The property can be one of the values defined in your pom.xml, a value defined in the user's settings.xml, a property defined in an external properties file, or a system property.

    2.1 语法 ${<property name>}

    1. one of the values defined in your pom.xml
    2. a value defined in the user's settings.xml
    3. a property defined in an external properties file
    4. a system property

    2.2 在 pom.xml 中定义property ,请参考pom.xml 的语法

        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>

    ?

    2.3 在 settings.xml 中定义 property ,请参考 MAVEN_HOME/conf/settings.xml 的语法

        <profile>
          <properties>
            <tomcatPath>/path/to/tomcat/instance</tomcatPath>
          </properties>
        </profile>

    2.4 在 .properties 文件中定义 property

    some.properties 位于CLASS_PATH 下

    svn.password=test

    2.5 在 system property

    启动jdk时 通过 -D 指定(还有其他的已有的不具体分析System.getProperties();)

    2.6 Java EL表达式

    http://docs.oracle.com/javaee/6/tutorial/doc/gjddd.html

    3 缺省值列表

    3.1 一些资料中的

    As you can see, the project created from the archetype has a POM, a source tree for your application's sources and a source tree for your test sources. This is the standard layout for Maven projects (the application sources reside in ${basedir}/src/main/java and test sources reside in ${basedir}/src/test/java, where ${basedir} represents the directory containing pom.xml).

    ${basedir}

    3.2 父 pom 中的

    父pom位置:

    %MAVEN_HOME%libmaven-model-builder-3.2.5.jarorgapachemavenmodelpom-4.0.0.xml

    <build>
      <directory>${project.basedir}/target</directory> <outputDirectory>${project.build.directory}/classes</outputDirectory> <finalName>${project.artifactId}-${project.version}</finalName> <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory> <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory> <scriptSourceDirectory>${project.basedir}/src/main/scripts</scriptSourceDirectory> <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>

    例如:project.build.directory

    3.3 为什么可以用 project.name

    Maven 源码

    https://github.com/apache/maven

    http://maven.apache.org/ref/3.2.3/apidocs/org/apache/maven/project/MavenProject.html

    DefaultProjectBuilder.java 构建 一个 MavenProject

    而MavenProject 所对应就是pom.xml

    既然是${project} 那就应该存在一个 getProject()

    上面这个类具体怎么执行没有深入调查,或许是这个类或许是其他类,但一定会有getProject() 这个方法来对应EL表达式

    这个对象中有这些 setter 和 getter

    ${project.name} 一定是这个

    那么这个project 中的 build 呢

    http://maven.apache.org/ref/3.2.3/apidocs/overview-summary.html

    org.apache.maven.model
    Maven POM (Project Object Model) classes, generated from maven.mdo model.

    这个包中的类是由 maven.mdo 生成的,也就是说下载Maven源码之后,需要执行安装,才能生成。

    org.apache.maven.model.Build 也是如此(Build 继承 BuildBase)

     结论 

    ${project.build.directory}

     等价于

    String projectBuildDirectory = getProject().getBuild().getDirectory();

    最后一个问题:basedir 在哪呢?

    在 project 中

    这就可以理解为什么是 ${basedir}了

    在哪里设置的呢?

    MavenProject 中没有setBasedir() 方法, 根据观察有下面这个方法

        public void setFile( File file )
        {
            this.file = file;
            this.basedir = file != null ? file.getParentFile() : null;
        }

    这个file 指的是当前项目的 pom.xml  (不是父pom)

     basedir 指的是包含 pom.xml 的文件夹

    4 总结

    这就是喜闻乐见的 target 为什么会和pom.xml 同级

    classes 为什么会在target下

    test-classes 为什么会在target下 的原因了

    这些属性是可以在各种plugin 中指定为自己的路径

  • 相关阅读:
    LeetCode题解(2)-- Add Two Numbers
    LeetCode题解(1)--Two Sum
    STL review:vector & string & map & struct
    机器学习导论(一) 基本概念
    Single Number
    Single Number II
    Copy List with Random Pointer
    Word Break
    Populating Next Right Pointers in Each Node II
    Binary Tree Inorder Traversal
  • 原文地址:https://www.cnblogs.com/zno2/p/5970487.html
Copyright © 2020-2023  润新知