一步步搭建采用Gradle来运行Spring Boot应用的生产环境

2019-08-23 10:30:10 浏览数 (1)

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在文件的最开始添加

代码语言:javascript复制
#@wjw_add
wrapper.ignore_sequence_gaps=TRUE 

3.2添加JVM参数

代码语言:javascript复制
#->@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注释掉

代码语言:javascript复制
#@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:

打开系统属性->环境变量->添加用户变量

代码语言:javascript复制
变量名: 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, 其默认的配置如下:

代码语言:javascript复制
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 发布包的位置.如果遇到下载这个发布包比较慢甚至不成功的时候,可以将该地址引到本地的文件,比如:

代码语言:javascript复制
#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文件模版

代码语言:javascript复制
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文件模版

代码语言:javascript复制
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(/ /,' ')   ' '
			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,文件类容是

代码语言:javascript复制
//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'

0 人点赞