https://www.baeldung.com/maven-settings-xml 구글번역 + 자체교정

 

 

1. 개요

Maven을 사용하는 동안 우리는 대부분의 프로젝트별 구성을 pom.xml에 유지합니다.
Maven은 사용할 로컬 및 원격 저장소를 지정할 수 있는 설정 파일인 settings.xml을 제공합니다. 자격 증명( credentials )과 같이 소스 코드에 원하지 않는 설정을 저장하는 데에도 사용할 수 있습니다.
이 튜토리얼에서는 settings.xml을 사용하는 방법을 배웁니다. 프록시, 미러링, 프로필을 살펴보겠습니다. 또한 프로젝트에 적용되는 현재 설정을 결정하는 방법에 대해서도 논의하겠습니다.

 

2. 구성

settings.xml 파일은 Maven 설치를 구성합니다. pom.xml 파일과 유사하지만 전역적으로 또는 사용자별로 정의됩니다.
settings.xml 파일에서 구성할 수 있는 요소를 살펴보겠습니다. settings.xml 파일의 기본 설정 요소에는 사전 정의된 9개의 하위 요소가 포함될 수 있습니다.

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
    <localRepository/>
    <interactiveMode/>
    <offline/>
    <pluginGroups/>
    <servers/>
    <mirrors/>
    <proxies/>
    <profiles/>
    <activeProfiles/>
</settings>

 

2.1. Simple Values

일부 최상위 구성 요소에는 간단한 값이 포함되어 있습니다.

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
    <localRepository>${user.home}/.m2/repository</localRepository>
    <interactiveMode>true</interactiveMode>
    <offline>false</offline>
</settings>

localRepository 요소는 시스템의 로컬 저장소 경로를 가리킵니다. 로컬 저장소는 프로젝트의 모든 종속성이 캐시되는 곳입니다. 기본값은 사용자의 홈 디렉터리를 사용하는 것입니다. 그러나 로그인한 모든 사용자가 공통 로컬 저장소에서 빌드할 수 있도록 이를 변경할 수 있습니다.
InteractiveMode 플래그는 Maven이 입력을 요청하는 사용자와 상호 작용할 수 있는지 여부를 정의합니다. 이 플래그의 기본값은 true입니다.
오프라인 플래그는 빌드 시스템이 오프라인 모드에서 작동할 수 있는지 여부를 결정합니다. 기본값은 false입니다. 그러나 빌드 서버가 원격 저장소에 연결할 수 없는 경우에는 이를 true로 전환할 수 있습니다.

 

2.2. Plugin Groups

PluginGroups 요소에는 groupId를 지정하는 하위 요소 목록이 포함되어 있습니다. groupId는 특정 Maven 아티팩트를 생성한 조직의 고유 식별자입니다.

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
    <pluginGroups>
        <pluginGroup>org.apache.tomcat.maven</pluginGroup>
    </pluginGroups>
</settings>

Maven은 명령줄에 제공된 groupId 없이 플러그인을 사용할 때 플러그인 그룹 목록을 검색합니다. 목록에는 기본적으로 org.apache.maven.plugins 및 org.codehaus.mojo 그룹이 포함되어 있습니다. 위에 정의된 settings.xml 파일을 사용하면 잘린 Tomcat 플러그인 명령을 실행할 수 있습니다. (잘린 tomcat 플러그인을 실행할 수 있다는 거는 밑에 명령어를 말하는 듯하다.)

mvn tomcat7:help
mvn tomcat7:deploy
mvn tomcat7:run

 

2.3. 프록시

Maven의 HTTP 요청 중 일부 또는 전부에 대해 프록시를 구성할 수 있습니다. 프록시 요소는 하위 프록시 요소 목록을 허용하지만 한 번에 하나의 프록시만 활성화할 수 있습니다.

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
    <proxies>
        <proxy>
            <id>baeldung-proxy</id>
            <active>true</active>
            <protocol>http</protocol>
            <host>baeldung.proxy.com</host>
            <port>8080</port>
            <username>demo-user</username>
            <password>dummy-password</password>
            <nonProxyHosts>*.baeldung.com|*.apache.org</nonProxyHosts>
        </proxy>
    </proxies>
</settings>

활성 플래그를 통해 현재 활성 프록시를 정의합니다. 그런 다음 nonProxyHosts 요소를 사용하여 프록시되지 않는 호스트를 지정합니다. 사용되는 구분 기호는 특정 프록시 서버에 따라 다릅니다. 가장 일반적인 구분 기호는 파이프(|)와 쉼표(,)입니다.

 

2.4. Mirrors

저장소는 pom.xml 프로젝트 내에서 선언될 수 있습니다. 이는 프로젝트 코드를 공유하는 개발자가 즉시 올바른 저장소 설정을 얻을 수 있음을 의미합니다.

특정 저장소에 대한 대체 미러를 정의하려는 경우 미러를 사용할 수 있습니다. 이는 pom.xml의 내용을 재정의합니다.
예를 들어, 모든 저장소 요청을 미러링하여 Maven이 단일 저장소를 사용하도록 강제할 수 있습니다.

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
    <mirrors>
        <mirror>
            <id>internal-baeldung-repository</id>
            <name>Baeldung Internal Repo</name>
            <url>https://baeldung.com/repo/maven2/</url>
            <mirrorOf>*</mirrorOf>
        </mirror>
    </mirrors>
</settings>


주어진 저장소에 대해 하나의 미러만 정의할 수 있으며 Maven은 첫 번째 일치 항목을 선택합니다. 일반적으로 CDN을 통해 전 세계적으로 배포되는 공식 저장소를 사용해야 합니다.

 

2.5. 서버

pom.xml 프로젝트에서 저장소를 정의하는 것은 좋은 습관입니다. 그러나 자격 증명( credentials )과 같은 보안 설정을 pom.xml을 사용하여 소스 코드 저장소에 넣어서는 안 됩니다. 대신 settings.xml 파일에 이 보안 정보를 정의합니다.

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
    <servers>
        <server>
            <id>internal-baeldung-repository</id>
            <username>demo-user</username>
            <password>dummy-password</password>
            <privateKey>${user.home}/.ssh/bael_key</privateKey>
            <passphrase>dummy-passphrase</passphrase>
            <filePermissions>664</filePermissions>
            <directoryPermissions>775</directoryPermissions>
            <configuration></configuration>
        </server>
    </servers>
</settings>

settings.xml의 서버 ID는 pom.xml에 언급된 저장소의 ID 요소와 일치해야 합니다. XML을 사용하면 placeholders 를 사용하여 환경 변수에서 자격 증명 ( credentials )을 선택할 수도 있습니다.

 

3. profile

profile 요소를 사용하면 ID 하위 요소로 구별되는 여러 프로필 하위 요소를 만들 수 있습니다. settings.xml의 profile 요소는 pom.xml에서 사용할 수 있는 동일한 요소의 잘린 버전입니다.
activationrepositoriespluginRepositories, and properties 의 네 가지 하위 요소만 포함할 수 있습니다. 이러한 요소는 특정 프로젝트 대신 빌드 시스템 전체를 구성합니다.
settings.xml의 활성 프로필 값이 pom.xml 또는 profile .xml 파일의 어떤 동등한 해당 profile 값보다 우선 적용된다는 점에 유의하는 것이 중요합니다. 프로필은 ID와 일치합니다.

 

3.1. Activation

특정 상황에서만 특정 값을 수정하기 위해 프로필을 사용할 수 있습니다. activation 요소를 사용하여 이러한 상황을 지정할 수 있습니다. 결과적으로 지정된 모든 기준이 충족되면 profile activation이 발생합니다.

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
    <profiles>
        <profile>
            <id>baeldung-test</id>
            <activation>
                <activeByDefault>false</activeByDefault>
                <jdk>1.8</jdk>
                <os>
                    <name>Windows 10</name>
                    <family>Windows</family>
                    <arch>amd64</arch>
                    <version>10.0</version>
                </os>
                <property>
                    <name>mavenVersion</name>
                    <value>3.0.7</value>
                </property>
                <file>
                    <exists>${basedir}/activation-file.properties</exists>
                    <missing>${basedir}/deactivation-file.properties</missing>
                </file>
            </activation>
        </profile>
    </profiles>
</settings>

네 가지 가능한 activators 가 있으며 모두 지정할 필요는 없습니다.

  • jdk: 지정된 JDK 버전에 따라 활성화됩니다(범위가 지원됨).
  • os: 운영 체제 속성에 따라 활성화됩니다.
  • property : Maven이 특정 속성 값을 감지하면 프로필을 활성화합니다.
  • file: 주어진 파일 이름이 존재하거나 누락된 경우 프로필을 활성화합니다.

어떤 프로필이 특정 빌드를 활성화할지 확인하려면 Maven 도움말 플러그인을 사용할 수 있습니다.

mvn help:active-profiles

출력에는 특정 프로젝트에 대해 현재 활성화된 프로필이 표시됩니다.

[INFO] --- maven-help-plugin:3.2.0:active-profiles (default-cli) @ core-java-streams-3 ---
[INFO]
Active Profiles for Project 'com.baeldung.core-java-modules:core-java-streams-3:jar:0.1.0-SNAPSHOT':
The following profiles are active:
 - baeldung-test (source: com.baeldung.core-java-modules:core-java-streams-3:0.1.0-SNAPSHOT)

 

3.2. Properties

Maven 속성은 특정 값에 대한 명명된 placeholders 로 생각할 수 있습니다. ${property_name} 표기법을 사용하여 pom.xml 파일 내에서 값에 액세스할 수 있습니다.

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
    <profiles>
        <profile>
            <id>baeldung-test</id>
            <properties>
                <user.project.folder>${user.home}/baeldung-tutorials</user.project.folder>
            </properties>
        </profile>
    </profiles>
</settings>

pom.xml 파일에서는 네 가지 유형의 속성을 사용할 수 있습니다.

  • env 접두사를 사용하는 속성은 ${env.PATH}와 같은 환경 변수 값을 반환합니다.
  • 프로젝트 접두사를 사용하는 속성은 ${project.version}과 같은 pom.xml의 프로젝트 요소에 설정된 속성 값을 반환합니다.
  • setting 접두사를 사용하는 속성은 ${settings.localRepository}와 같은 settings.xml에서 해당 요소의 값을 반환합니다.
  • ${java.home}과 같이 Java의 System.getProperties 메소드를 통해 사용 가능한 모든 속성을 직접 참조할 수 있습니다.
  • ${junit.version}과 같이 접두사가 없는 속성 요소 내에 설정된 속성을 사용할 수 있습니다.

3.3. Repositories

원격 저장소에는 Maven이 로컬 저장소를 채우는 데 사용하는 아티팩트 모음이 포함되어 있습니다. 특정 아티팩트에는 다른 원격 저장소가 필요할 수 있습니다. Maven은 활성 프로필에서 활성화된 저장소를 검색합니다.

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
    <profiles>
        <profile>
            <id>adobe-public</id>
            <repositories>
	        <repository>
	            <id>adobe-public-releases</id>
	            <name>Adobe Public Repository</name>
	            <url>https://repo.adobe.com/nexus/content/groups/public</url>
	            <releases>
	                <enabled>true</enabled>
	                <updatePolicy>never</updatePolicy>
	            </releases>
	            <snapshots>
	                <enabled>false</enabled>
	            </snapshots>
	        </repository>
	    </repositories>
        </profile>
    </profiles>
</settings>

repository 요소를 사용하여 특정 저장소의 아티팩트 릴리스 또는 스냅샷 버전만 활성화할 수 있습니다.

 

3.4. Plugin Repositories

Maven 아티팩트에는 종속성 및 플러그인이라는 두 가지 표준 유형이 있습니다. Maven 플러그인은 특별한 유형의 아티팩트이므로 플러그인 저장소를 다른 저장소와 분리할 수 있습니다.

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
    <profiles>
        <profile>
            <id>adobe-public</id>
            <pluginRepositories>
               <pluginRepository>
                  <id>adobe-public-releases</id>
                  <name>Adobe Public Repository</name>
                  <url>https://repo.adobe.com/nexus/content/groups/public</url>
                  <releases>
                      <enabled>true</enabled>
                      <updatePolicy>never</updatePolicy>
                  </releases>
                  <snapshots>
                      <enabled>false</enabled>
                  </snapshots>
	        </pluginRepository>
	    </pluginRepositories>
        </profile>
    </profiles>
</settings>

pluginRepositories 요소의 구조는 repositories 요소와 매우 유사합니다. (위의 코드블럭 repositories element와 구조가 유사)

 

3.5. Active Profiles

activeProfiles 요소에는 특정 프로필 ID를 참조하는 하위 요소가 포함되어 있습니다. Maven은 여기에 참조된 모든 프로필을 자동으로 활성화합니다.

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
    <activeProfiles>
        <activeProfile>baeldung-test</activeProfile>
        <activeProfile>adobe-public</activeProfile>
    </activeProfiles>
</settings>

 

이 예에서 mvn의 모든 호출은 명령줄에 -P baeldung-test,adobe-public을 추가한 것처럼 실행됩니다.

 

4. Settings Level

settings.xml 파일은 일반적으로 다음 두 위치에서 찾을 수 있습니다. 

  • Mavens 홈 디렉터리의 전역 설정: ${maven.home}/conf/settings.xml
  • 사용자 설정: ${user.home}/.m2/settings.xml

두 파일이 모두 존재하면 해당 내용이 병합됩니다. 사용자 설정의 구성이 우선적으로 적용됩니다.

 

4.1. Determine File Location

전역 및 사용자 설정의 위치를 ​​결정하기 위해 디버그 플래그를 사용하여 Maven을 실행하고 출력에서 ​​"설정"을 검색할 수 있습니다.

mvn -X clean | grep "settings"

[DEBUG] Reading global settings from C:\Program Files (x86)\Apache\apache-maven-3.6.3\bin\..\conf\settings.xml
[DEBUG] Reading user settings from C:\Users\Baeldung\.m2\settings.xml

 

4.2. Determine Effective Settings

Maven 도움말 플러그인을 사용하여 결합된 전역 및 사용자 설정의 내용을 확인할 수 있습니다.

mvn help:effective-settings

다음은 XML 형식의 설정을 설명합니다.

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
    <localRepository>C:\Users\Baeldung\.m2\repository</localRepository>
    <pluginGroups>
        <pluginGroup>org.apache.tomcat.maven</pluginGroup>
        <pluginGroup>org.apache.maven.plugins</pluginGroup>
        <pluginGroup>org.codehaus.mojo</pluginGroup>
    </pluginGroups>
</settings>

 

4.3. Override the Default Location

Maven을 사용하면 명령줄을 통해 전역 및 사용자 설정의 위치를 ​​재정의할 수도 있습니다.

mvn clean --settings c:\user\user-settings.xml --global-settings c:\user\global-settings.xml
// --setting 과 -s 가 같은 옵션
mvn clean --s c:\user\user-settings.xml --gs c:\user\global-settings.xml

 

5. Conclusion

이 기사에서는 Maven의 settings.xml 파일에서 사용할 수 있는 구성을 살펴보았습니다. 프록시, 리포지토리 및 프로필을 구성하는 방법을 배웠습니다. 다음으로 전역 설정 파일과 사용자 설정 파일의 차이점과 사용 중인 파일을 확인하는 방법을 살펴보았습니다. 마지막으로 사용되는 효과적인 설정을 결정하고 기본 파일 위치를 재정의하는 방법을 살펴보았습니다.

'정보 > Server' 카테고리의 다른 글

Java vm option [argument] 확인하기  (0) 2024.05.23
[Project] maven setting.xml  (0) 2024.04.26
[Project] Maven Packaging Types  (0) 2024.04.26
[Project] Accessing Maven Properties in Java  (0) 2024.04.25
[Project] Maven Properties Defaults  (1) 2024.04.25

+ Recent posts