카테고리 없음

Maven - build, compiler,settings.xml

02O2 2022. 6. 15. 20:25

Maven이란 빌드(build) 툴의 일종이다. 빌드란 소스 코드 파일을 JVM이나 WAS가 인식할 수 있는 구조로 패키징 하는 과정 및 결과를 말한다. 메이븐을 사용하면 프로젝트 생성, 라이브러리 설정, 코드 작업 등을 쉽게 할 수 있다. 이클립스와 같은 IDE에서도 프로젝트 생성 등을 할 수 있지만 IDE는 빌드 툴과는 다르다. 빌드 툴이 빌드 과정에서의 프로젝트 생성, 라이브러리 설정 등에 관여할 수 있다면 IDE는 더 다양한 범위를 포괄하는 더 큰 단위의 도구이다. IDE는 형상관리, 디버거, 컴파일러, 탐색기, 콘솔, 편집기 등의 기능을 포함한다. Maven은 특정한 IDE에 종속된 것이 아니며 이클립스, Visual Studio Code, IntellijJ 등 여러 IDE는 전부 메이븐을 이용할 수 있다.

 

 

빌드 및 패키징

앞에서 만든 메이븐 프로젝트를 빌드하고 패키징하는 일이 남았다.

모든 동작은 POM파일에 작성되어 있다.

많은 빌드 소프트웨어의 스크립트는 클래스 패스를 설정하고 정보를 설정하고 컴파일 방법과 패키징 방법을 기술하도록 되어 있는데(예 : ANT빌드) pom.xml파일에는 그런 내용이 없다.(늘상 반복적으로 정의하는 빌드 프로세스가 내장되어 있고 그래서 각 명령에 대한 상세 정의 역시 존재하지 않는다.)

 

 

 

 

컴파일러(compiler)

  • 기본적으로 maven은 jdk 1.5 기준으로 compile을 시도한다.
  • 1.5버전보다 더 높은 버전으로 compile 하기위해서는 다음과 같은 조치가 필요하다
<plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
           <source>1.8</source>
           <target>1.8</target>
        </configuration>
  </plugin>

Maven이 기본적으로 사용하는 소스 및 생성 클래스의 자바 버전을 변경하려면 pom.xml 파일에서 maven-compiler-plugin의 <configuration> 영역에서 <source>와 <target>을 이용해서 소스 코드 및 생성 대상 자바 버전을 지정할 수 있다.

 

 

외부라이브러리추가(빌드패스(?))

  <dependencies>
     <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.12.0</version>
     </dependency>
  </dependencies>
</project>

commons-lang3 라이브러리를 pom.xml에 추가한 dependency이다.

Maven을 이용해 Dependency를 추가하여 라이브러리를 사용

개발환경을 구축하고 여기있는 디펜던스로 api를 글어다가 쓴다.

 

메이븐의 있는 모든 툴은 settings.xml로 

 

settings.xml은 무슨 설정을 위한 파일일까?

  • Maven 실행에 필요한 설정들을 정의하는 파일입니다.
  • 그럼 왜 pom.xml에 안 넣고 settings.xml을 따로 만들었을까?
    • 특정 프로젝트에 종속되는 정보가 아니다.

settings.xml은 어디곳에 있을까?

  • ${user.home}/.m2/settings.xml
    • default path
  • ${maven.home}/conf/settings.xml
  • 명령어를 통해 custom settings.xml을 적용할 수 있습니다.
    • mvn package -s ./setting/settings.xml

settings.xml은 무슨 정보를 가지고 있을까?

Simple Values

  • localRepository
    • maven 로컬 저장소 위치
  • interactiveMode
    • 사용자의 input에 움직있는 모드로 변경
    • default true
  • offline
    • remote 저장소에 연결하지 않고 빌드를 하기 위한 옵션
    • default false
<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>

Plugin Groups

  • pom.xml에 groupId가 설정되어 있지 않을 때 선언된 GroupId로 플러그인을 탐색
    • plugin에 groupId를 표시했다면 아무 기능도 하지 않음.
<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.eclipse.jetty</pluginGroup>
  </pluginGroups>
  ...
</settings>

Servers

  • 저장소 서버에 관련된 설정
    • 저장소(nexus)의 인증하는 방법을 제공합니다.
<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>server001</id>
      <username>my_login</username>
      <password>my_password</password>
      <privateKey>${user.home}/.ssh/id_dsa</privateKey>
      <passphrase>some_passphrase</passphrase>
      <filePermissions>664</filePermissions>
      <directoryPermissions>775</directoryPermissions>
      <configuration></configuration>
    </server>
  </servers>
  ...
</settings>

Mirros

<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>planetmirror.com</id>
      <name>PlanetMirror Australia</name>
      <url>http://downloads.planetmirror.com/pub/maven2</url>
      <mirrorOf>central</mirrorOf>
    </mirror>
  </mirrors>
  ...
</settings>

Proxies

  • 방화벽이나 SSL 이슈를 해결하기 위해 Proxy 설정을 합니다.
<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>myproxy</id>
      <active>true</active>
      <protocol>http</protocol>
      <host>proxy.somewhere.com</host>
      <port>8080</port>
      <username>proxyuser</username>
      <password>somepassword</password>
      <nonProxyHosts>*.google.com|ibiblio.org</nonProxyHosts> // 해당 도메인은 Proxy 적용이 되지 않습니다.
    </proxy>
  </proxies>
  ...
</settings>

Profiles

  • JDK 버젼, OS에 맞게 빌드 구성을 다르게 설정할 수 있습니다.
<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>test</id>
      <activation>
        <activeByDefault>false</activeByDefault>
        <jdk>1.5</jdk>
        <os>
          <name>Windows XP</name>
          <family>Windows</family>
          <arch>x86</arch>
          <version>5.1.2600</version>
        </os>
        <property>
          <name>mavenVersion</name>
          <value>2.0.3</value>
        </property>
        <file>
          <exists>${basedir}/file2.properties</exists>
          <missing>${basedir}/file1.properties</missing>
        </file>
      </activation>
      ...
    </profile>
  </profiles>
  ...
</settings>

Active Profiles

  • 위에 선언한 Profile을 정의하면 해당 Profile로 빌드가 됩니다.
  • 여러 Profile을 선언하면 순서대로 빌드가 됩니다.
<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>env-test</activeProfile>
  </activeProfiles>
</settings>

Reference