一步步搭建採用Gradle來運行Spring Boot應用的生產環境 頂 原

1.搭建Maven私服

  1. 創建/opt/nexus/目錄做爲nexus的根目錄,把nexus-2.14.5-02-bundle.tar.gz解壓到此目錄中
  2. 修改/opt/nexus/nexus-2.14.5-02/conf/nexus.properties文件,把application-port設置成合適的端口
  3. 修改/opt/nexus/nexus-2.14.5-02/bin/jsw/conf/wrapper.conf文件,配置合適的JVM參數.

3.1在文件的最開始添加

#@wjw_add
wrapper.ignore_sequence_gaps=TRUE 


3.2添加JVM參數

#->@wjw_add
wrapper.java.additional.11=-Djava.net.preferIPv4Stack=true
wrapper.java.additional.12=-Dcom.sun.jndi.ldap.connect.pool.protocol="plain ssl"
wrapper.java.additional.13=-server
wrapper.java.additional.14=-Xms1g
wrapper.java.additional.15=-Xmx8g
wrapper.java.additional.16=-XX:ReservedCodeCacheSize=96m
#<-@wjw_add

3.3註釋掉

#@wjw_note wrapper.java.initmemory=256

#@wjw_note wrapper.java.maxmemory=768
  1. 如果需要以root啓動,修改/opt/nexus/nexus-2.14.5-02/bin/nexus文件,去掉RUN_AS_USER的註釋,改成RUN_AS_USER=root
  2. 啓動nexus,/opt/nexus/nexus-2.14.5-02/bin/nexus start

2. 配置Gradle環境

Linux:

  1. 創建/opt/GRADLE_USER_HOME目錄
  2. 修改/etc/profile,在最後添加export > GRADLE_USER_HOME=/opt/GRADLE_USER_HOME
  3. 執行source /etc/profile,是配置環境生效

Windows:

打開系統屬性->環境變量->添加用戶變量

變量名: GRADLE_USER_HOME
變量值: z:\GRADLE_USER_HOME

3. 讓Gradle Wrapper引用本地的發佈包

Gradle Wrapper 免去了用戶在使用 Gradle 進行項目構建時需要安裝 Gradle 的繁瑣步驟. 每個 Gradle Wrapper 都綁定到一個特定版本的 Gradle,所以當你第一次在給定 Gradle 版本下運行上面的命令之一時,它將下載相應的 Gradle 發佈包,並使用它來執行構建.默認,Gradle Wrapper 的發佈包是指向的官網的 Web 服務地址,有時候,下載這個發佈包比較慢甚至不成功,本文演示了加速下載發佈包的方式.

Gradle Wrapper 的配置在gradle/wrapper/gradle-wrapper.properties, 其默認的配置如下:

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-bin.zip

其中,distributionUrl 指 明瞭 Gradle Wrapper 下載 Gradle 發佈包的位置.如果遇到下載這個發佈包比較慢甚至不成功的時候,可以將該地址引到本地的文件,比如:

#distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-bin.zip
distributionUrl=file\:/D:/software/webdev/java/gradle-3.5-all.zip

這樣構建的速度將會非常快了.當然,前提是,要實現準本好發佈包放到本地.

4. Spring Bootbuild.gradle文件模版

buildscript {
	ext {
		springBootVersion = '1.5.8.RELEASE'
	}
	repositories {
		mavenLocal()
		maven{ url "http://SVN:8081/nexus/content/groups/public"}
		mavenCentral()
		jcenter()
	}
	dependencies {
		classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
	}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

group = 'org.wjw.EurekaServer'
version = '1.0.0'

sourceCompatibility = 1.8
targetCompatibility = 1.8
[compileJava, compileTestJava, javadoc]*.options*.encoding = 'UTF-8'

repositories {
    mavenLocal()
	maven{ url "http://SVN:8081/nexus/content/groups/public"}
	mavenCentral()
	jcenter()
}


ext {
	springCloudVersion = 'Dalston.SR4'
}

dependencies {
	compile('org.springframework.cloud:spring-cloud-starter-eureka-server') {
	  exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
	}
	compile('org.slf4j:slf4j-api')
	compile('org.slf4j:slf4j-log4j12')
	compile('commons-logging:commons-logging')

	compile('org.springframework.boot:spring-boot-starter-actuator') {
	  exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
	}

	compile('org.springframework.boot:spring-boot-starter-security') {
	  exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
	}
	testCompile('org.springframework.boot:spring-boot-starter-test')
}

dependencyManagement {
	imports {
		mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
	}
}

//bootRun {
//    args = ["--spring.profiles.active=test"]
//}
//@wjw_note 添加接受JVM命令行參數,例如:-PjvmArgs="-XX:ReservedCodeCacheSize=96m -Xmx1g"
//@wjw_note 添加接受project命令行參數,例如:-PappArgs="--spring.profiles.active=dev"
bootRun {
	if ( project.hasProperty('jvmArgs') ) {
		jvmArgs = (project.jvmArgs.split("\\s+") as List)
	}
		
	if(project.hasProperty("appArgs")){
	  args(appArgs)
	  println "Task args:"+args
	}
}

5. Jar Libbuild.gradle文件模版

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'maven'

group = 'org.wjw.cloud.config'
version = '1.0.0'
  
sourceCompatibility = 1.6
targetCompatibility = 1.6

[compileJava, compileTestJava, javadoc]*.options*.encoding = 'UTF-8'

repositories {
  mavenLocal()
  maven{ url "http://SVN:8081/nexus/content/groups/public"}
  mavenCentral()
  jcenter()
}


dependencies {
	compile('io.github.openfeign:feign-core:9.5.0')
	compile('io.github.openfeign:feign-ribbon:9.5.0')
	testCompile('junit:junit:4.12')
}

jar.doFirst {
	manifest {
		def manifestFile = "${projectDir}/META-INF/MANIFEST.MF"
		if ( new File( manifestFile ).exists() ) {
			from ( manifestFile )
		}	
		def requiredProjects = ''
		configurations.compile.getAllDependencies().withType(ProjectDependency).each {dep->
			def dependantProjects = dep.getDependencyProject()
			def projects = project(dependantProjects.path).libsDir.list().findAll{it.endsWith('.jar')}
			projects.removeAll(projects.findAll{it.endsWith('test.jar')})
			def requiredProject = projects.join(' ')
			requiredProjects +=  requiredProject.replaceAll(/ /,'%20') + ' '
			logger.info 'Required Project: ' + requiredProject
		}
		logger.info 'Required requiredProjects: ' + requiredProjects

		def compileFiles = configurations.compile.files{ it instanceof ExternalDependency }.collect  {
			File file = it
			"${file.name}"
		}.join(' ')

		def manifestPath = requiredProjects + compileFiles
		logger.info 'Manifest: '+ manifestPath
		attributes 'Dependency-Libs': manifestPath
		attributes 'Build-Date': new Date();
		attributes 'Implementation-Title': project.name
		attributes 'Application-Version': project.version
	}
}

uploadArchives {
	repositories.mavenDeployer {
	  repository(url: "http://svn:8081/nexus/content/repositories/thirdparty/") {
		authentication(userName: "wangjunwei", password: "xxxxxx")
	  }
	  
	  pom.groupId = "${project.group}"
	  pom.artifactId = "${project.name}"
	  pom.version = "${project.version}"
	  pom.project {
		name project.name
		packaging 'jar'
		description '封裝了spring cloud config server,可以很簡單的獲取配置信息!'
		
		licenses {
		  license {
			name 'The Apache Software License, Version 2.0'
			url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
			distribution '封裝了spring cloud config server,可以很簡單的獲取配置信息!'
		  }
		}
	  
		developers {
		  developer {
			id 'wangjunwei'
			name 'Wang JunWei'
		  }
		}
	  }
	}
}

//爲項目生成**.jar/**-javadoc.jar/**-sources.jar
task javadocJar(type: Jar, dependsOn: javadoc) {
	classifier = 'javadoc'
	from 'build/docs/javadoc'
}
  
task sourcesJar(type: Jar) {
	classifier = 'sources'
	from sourceSets.main.allSource
}

artifacts {
	archives jar
	archives javadocJar
	archives sourcesJar
}

5. 自己定製project.name

在項目目錄下創建settings.gradle,文件類容是

//By default, Gradle uses the directory name as project name. 
//You can change this by creating a settings.gradle file in the directory which specifies the project name.
rootProject.name ='org.wjw.cloud.CloudConfigService'
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章