7.1 Maven의 변수 - 속성

자주 발생하는 매개변수 Maven을 사용하면 변수에 넣을 수 있습니다. 이는 pom 파일의 다른 부분에 있는 매개변수를 일치시켜야 할 때 매우 유용합니다. 예를 들어 Java 버전, 라이브러리 버전, 특정 리소스에 대한 경로를 변수에 넣을 수 있습니다.

pom.xml – <properties>이를 위해 에는 변수가 선언되는 특수 섹션이 있습니다 . 변수의 일반적인 형식은 다음과 같습니다.

<variable-name> _ _ _ _meaning< / variable name > _

예:

<properties>
    <junit.version>5.2</junit.version>
    <project.artifactId>new-app</project.artifactId>
    <maven.compiler.source>1.13</maven.compiler.source>
    <maven.compiler.target>1.15</maven.compiler.target>
</properties>

변수는 다른 구문을 사용하여 액세스됩니다.

$ { variable -name } _

그러한 코드가 작성된 곳에서 Maven은 변수의 값을 대체합니다.

예:

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>
 
<build>
    <finalName>${project.artifactId}</finalName>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
            <source>${maven.compiler.source}</source>
            <target>${maven.compiler.target}</target>
        </configuration>
    </plugin>
</build>

7.2 Maven의 미리 정의된 변수

pom 파일에서 프로젝트를 설명할 때 미리 정의된 변수를 사용할 수 있습니다. 조건부로 여러 그룹으로 나눌 수 있습니다.

  • 내장 프로젝트 속성
  • 프로젝트 속성;
  • 설정.

기본 제공 프로젝트 속성은 두 가지뿐입니다.

재산 설명
${basedir} 프로젝트 루트 디렉토리pom.xml
${버전} 아티팩트 버전; 사용할 수 ${project.version}있거나${pom.version}

«project»프로젝트 속성은 또는 접두사를 사용하여 참조할 수 있습니다 «pom». 네 가지가 있습니다.

재산 설명
${프로젝트.빌드.디렉토리} «target»프로젝트 디렉토리
${project.build.outputDirectory} «target»컴파일러 디렉토리. 기본«target/classes»
${프로젝트 이름} 프로젝트의 이름
${프로젝트.버전} 프로젝트 버전

속성은 settings.xml접두사를 사용하여 액세스할 수 있습니다 settings. 이름은 무엇이든 될 수 있습니다. 에서 가져온 것입니다 settings.xml. 예:

${settings.localRepository} sets the path to the local repository.