반응형

※ 레거시 프로젝트를 운영하다가 정리하게 되었다.

1. pom.xml 에 Nexus 관련 설정을 추가한다

<distributionManagement>
	<repository>
		<id>nexus-releases</id>
		<url>http://${NEXUS-HOST}/nexus/content/repositories/releases/</url>
	</repository>

	<snapshotRepository>
		<id>nexus-snapshots</id>
		<url>http://${NEXUS-HOST}/nexus/content/repositories/snapshots/</url>
	</snapshotRepository>
</distributionManagement>

 

  • 여기서 id를 주목해야한다. 3번의 setting.xml 의 id와 동일해야한다.
  • 또한 대표저장소의 id 나 name 속성과 관련이없다.

 

2. pom.xml 에 Plugin 을 추가한다.

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-deploy-plugin</artifactId>
<!--				<version>${maven-deploy-plugin.version}</version>-->
	<configuration>
		<skip>true</skip>
	</configuration>
</plugin>

<plugin>
	<groupId>org.sonatype.plugins</groupId>
	<artifactId>nexus-staging-maven-plugin</artifactId>
	<version>1.5.1</version>
	<executions>
		<execution>
			<id>default-deploy</id>
			<phase>deploy</phase>
			<goals>
				<goal>deploy</goal>
			</goals>
		</execution>
	</executions>
	<configuration>
		<serverId>nexus</serverId>
		<nexusUrl>http://${NEXUS-HOST}/nexus/</nexusUrl>
		<skipStaging>true</skipStaging>
	</configuration>
</plugin>

 

3. 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/>-->
<!--	<interactiveMode/>-->
<!--	<offline/>-->
<!--	<pluginGroups/>-->
	<servers>
		<server>
			<id>nexus-releases</id>
			<username>${id}</username>
			<password>${password}</password>
		</server>
		<server>
			<id>nexus-snapshots</id>
			<username>${id}</username>
			<password>${password}</password>
		</server>
	</servers>
<!--	<mirrors/>-->
<!--	<proxies/>-->
<!--	<profiles/>-->
<!--	<activeProfiles/>-->
</settings>

※ settings.xml 참고 링크 - maven.apache.org/settings.html#Quick_Overview

  • 위에도 언급했지만 id를 주목해야한다. pom.xml 에 설정한 id값과 동일해야한다.
  • 또한 대표저장소의 id 나 name 속성과 관련이없다.

 

4. mvn 명령어를 통해 빌드 결과물을 넥서스에 배포한다.

옵션을 주지않으면 테스트가 실행이 될텐데 아래의 옵션을 통해 스킵할 수 있다.
mvn clean deploy
mvn clean deploy -Dmaven.test.skip=true

 

※ deploy 명령어 참고 링크 - maven.apache.org/guides/mini/guide-3rd-party-jars-remote.html

 

Maven – Guide to deploying 3rd party JARs to remote repository

Guide to deploying 3rd party JARs to remote repository Same concept of the install:install-file goal of the maven-install-plugin where the 3rd party JAR is installed in the local repository. But this time instead to local repository the JAR will be install

maven.apache.org

배포 작업의 경우에는 이 작업이 배포 파이프라인의 마지막 작업이어야 하므로 테스트를 건너뛰어도 좋습니다.  이러한 배포 파이프라인의 일반적인 예로는 Jenkins 작업의 연속이 있으며, 각 작업은 성공적으로 완료될 경우에만 다음 작업을 트리거합니다. 따라서 배포 작업이 실행될 때까지 프로젝트의 모든 테스트 제품군을 실행하는 것은 파이프라인의 이전 작업의 책임입니다. 모든 테스트는 이미 통과해야 합니다.

 

5. 결론

Maven 아티팩트를 Nexus로 배포하기 위한 간단하면서도 매우 효과적인 솔루션입니다. 또한 기본 maven-deploy-plugin 대신 nexus-staging-maven-plugin이 사용되고 스테이징 기능이 비활성화되는 등 어느 정도 의견이 분분합니다. 이러한 선택으로 솔루션을 간단하고 실용적으로 만들 수 있습니다.

 

참고자료) www.baeldung.com/maven-deploy-nexus

 

Maven Deploy to Nexus | Baeldung

Maven Deploy to Nexus - The Nexus Snapshot Repository in the pom and how to set up the Deployment Process.

www.baeldung.com

#

반응형
반응형

Gradle 간단히 사용하기.


Maven 은 사용한지 오래되기도 했고 익숙하지만 Gradle 아직 회사에서 사용을 안 하여;; ㅠㅠ ;;잘 잊어버리곤해서 간단한 Gradle 설치와 실행을 확인해 보고자 한다.


1. Gradle 설치 및 간단한 실행.

  • 먼저 Gradle 사이트에 접속한다.(https://gradle.org/gradle-download/)
  • sdkman 을 사용하던지, 개별적으로 download 하여 $PATH 와 $CLASSPATH 를 설정한다.(나는 후자)
  • 간단한 java project 를 생성 및 Helloworld.java 를 만들고, 프로젝트 폴더 안에 build.gradle 파일을 생성한다.
  • 생성한 build.gradle 에 apply plugin: 'java' 를 한 줄 넣는다.
  • gradle tasks 명령어를 실행해서 콘솔에 뜨는 목록들을 확인해 본다.
  •  gradle build 명령어를 사용하면 test, compile 후 jar 파일이 만들어 지는 것을 볼 수 있다.

실행한 java 파일은 다음과 같습니다.

package main.java.hello;


import org.joda.time.LocalTime;


public class HelloWorld {

public static void main(String[] args) {

LocalTime currentTime = new LocalTime();

System.out.println("The current local time is: " + currentTime);

Greeter greeter = new Greeter();

System.out.println(greeter.sayHello());

}

}


class Greeter {

public String sayHello() {

return "Hello world!";

}

}



2. Gradle 의존 파일들 추가하기.

 

Maven Central 을 추가했듯이 다음과 같이 build.gradle 파일에 추가한다.

repositories {

    mavenCentral()

}


필요한 dependencis 도 추가한다.

sourceCompatibility = 1.8

targetCompatibility = 1.8


dependencies {

    compile "joda-time:joda-time:2.2"

    testCompile "junit:junit:4.12"

}


그리고 JAR Artifact 의 이름도 정의한다.

jar {

    baseName = 'java-test'

    version = '0.1.0'

}


다시 gradle build 를 수행하면 정상적으로 수행된다. (dependency 된 import 는 주석처리)



3. 빌드를 쉽게 도와주는 Gradle Wrapper.


다음의 스크립트를 사용하면 시스템에 Gradle을 설치하지 않고도 Gradle 빌드를 수행할 수 있습니다.

$ gradle wrapper --gradle-version 3.3


생성된 명령어로 빌드를 할 수 있습니다.

./gradlew build


생성된 jar 를 실행해 봅니다. 

$ jar tvf build/libs/java-test-0.1.0.jar

0 Thu Jan 05 01:42:22 KST 2017 META-INF/

25 Thu Jan 05 01:01:30 KST 2017 META-INF/MANIFEST.MF

0 Thu Jan 05 01:01:30 KST 2017 main/

0 Thu Jan 05 01:01:30 KST 2017 main/java/

0 Thu Jan 05 01:42:22 KST 2017 main/java/hello

389 Thu Jan 05 01:42:22 KST 2017 main/java/hello/Greeter.class

1028 Thu Jan 05 01:42:22 KST 2017 main/java/hello/HelloWorld.class


작업한 코드를 실행할 수 있게 하려면 gradle 의 applicaiton 플러그인을 추가해야 합니다. 

build.gradle 파일에 다음과 같이 추가하세요.

apply plugin: 'application'

mainClassName = 'hello.HelloWorld'


자 이제 다시 실행해 봅시다.

$ ./gradlew run


지금은 jar 로 실행했는데요. 

war 로 실행되기를 원하면, 플러그인을 추가하면 됩니다. (https://docs.gradle.org/current/userguide/war_plugin.html)


다 만들어진 build.gradle 파일은 아래처럼 될 것 같습니당.


apply plugin: 'java'

apply plugin: 'application'

mainClassName = 'main.java.hello.HelloWorld'


// For starters, you need to add a source for 3rd party libraries.

// tag::repositories[]

repositories {

    mavenCentral()

}

// end::repositories[]


// tag::jar[]

jar {

    baseName = 'java-test'

    version =  '0.1.0'

}

// end::jar[]


// tag::dependencies[]

sourceCompatibility = 1.8

targetCompatibility = 1.8


dependencies {

    compile "joda-time:joda-time:2.2"

    testCompile "junit:junit:4.12"

}

// end::dependencies[]





반응형

+ Recent posts