개발

젠킨스 pipeline script, settings.xml, pom.xml 샘플

피터JK 2025. 9. 24. 12:24
728x90

젠킨스 pipeline script 샘플

pipeline {
    agent any
	tools {
		jdk 'jdk-8'				// Jenkins에서 설정한 JDK 이름
        maven 'maven 3.9.11' 	// Jenkins에서 설정한 Maven 이름
    }
    stages {
        stage('Git Checkout') {
            steps {
                git branch: 'master', credentialsId: 'git-deploy', url: 'http://172.17.0.4:80/test/egov-test.git'
            }
        }
        stage('Build') {
            steps {
                sh script:'mvn clean package --settings /var/jenkins_home/.m2/settings.xml'
            }
        }
		stage('Nexus Upload') {
            steps {
                // withMaven 대신 sh 명령어로 직접 업로드
                sh 'mvn deploy --settings /var/jenkins_home/.m2/settings.xml'
            }
        }
		stage('Deploy to Tomcat') {
		    steps {
		        //sh 'mvn tomcat7:undeploy --settings /var/jenkins_home/.m2/settings.xml || true'
        		//sh 'mvn tomcat7:deploy --settings /var/jenkins_home/.m2/settings.xml'
        		//sh 'cat /var/jenkins_home/.m2/settings.xml'
        		sh 'mvn tomcat7:redeploy -X --settings /var/jenkins_home/.m2/settings.xml'
		    }
		}
    }
}

 

 

젠킨스 (/var/jenkins_home/.m2/settings.xml) 샘플

<settings xmlns="http://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd">

<localRepository>/var/jenkins_home/.m2/repository</localRepository>
	<servers>
		<server>
			<id>nexus-public</id>
            <username>deploy</username>
            <password>deploy</password>
	    </server>
        <server>
            <id>3rdparty</id>
            <username>deploy</username>
            <password>deploy</password>
        </server>
        <server>
            <id>releases</id>
            <username>deploy</username>
            <password>deploy</password>
        </server>
        
        <server>
            <id>snapshots</id>
            <username>deploy</username>
            <password>deploy</password>
		</server>
		<!-- 톰캣 서버 접속 정보 -->
        <server>
            <id>tomcat-server</id>
            <username>deploy</username>
            <password>deploy</password>
        </server>
    </servers>

	<mirrors>
    	<mirror>
            <id>nexus-public</id>
            <url>http://172.30.1.5:8081/repository/maven-public/</url>
            <mirrorOf>*,!central</mirrorOf> <!-- *은 모든 저장소를 의미하고, !central은 Maven의 기본 중앙 저장소(central)를 제외 -->
        </mirror>
	</mirrors>

    <profiles>
        <profile>
			<id>nexus</id>
            <repositories>
                <repository>
                    <id>nexus-public</id>
                    <url>http://172.30.1.5:8081/repository/maven-public/</url>
                    <releases>
                    	<enabled>true</enabled>
                    </releases>
                    <snapshots>
                   		<enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
            
        	<pluginRepositories>
                <pluginRepository>
                    <id>nexus-public</id>
                    <url>http://172.30.1.5:8081/repository/maven-public/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </pluginRepository>
        	</pluginRepositories>
        </profile>
    </profiles>

    <activeProfiles>
        <activeProfile>nexus</activeProfile>
    </activeProfiles>

</settings>

 

pom.xml 샘플

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.example</groupId>
	<artifactId>egov-test</artifactId>
	<packaging>war</packaging>
	<version>1.0.14-SNAPSHOT</version>
	<name>egov-test</name>
	<url>http://www.egovframe.go.kr</url>
    
    <!-- nexus 저장소 지정 -->
    <repositories>
		<repository>
			<id>central</id>
			<url>http://localhost:8081/repository/maven-public/</url>
			<releases>
				<enabled>true</enabled>
			</releases>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		/</repository>
    </repositories>    
	<!-- dependencies 생략함 -->
    
    
    <!-- 톰캣 연결 설정 -->
    <build>
    <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
            <url>http://172.17.0.3:8080/manager/text</url> <!-- 톰캣 서버-->
            <server>tomcat-server</server> <!-- settings.xml 서버 id -->
            <path>/</path> <!-- context root -->
            <update>true</update>
        </configuration>
    </plugin>
    </build>

    <!-- Nexus 서버의 URL을 설정함으로써, 개발자가 mvn deploy 명령어를 실행할 때 빌드된 아티팩트가 Maven Central Repository가 아닌 사내 Nexus 서버로 업로드되도록 지정 -->
    <distributionManagement>
        <repository>
            <id>releases</id>
            <name>Nexus Releases Repository</name>
            <url>http://172.30.1.5:8081/repository/maven-releases/</url>
        </repository>
        <snapshotRepository>
            <id>snapshots</id>
            <name>Nexus Snapshots Repository</name>
            <url>http://172.30.1.5:8081/repository/maven-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>

</project>
728x90