2018년 7월 16일 월요일

[Java] Build Project with Gradle


Downloadgradle-4.9-bin.zip gradle-4.9-all.zip

1. Create Project

- Make an empty project directory
- Create project skeleton with 'gradle init' command in the project directory.
1
2
3
4
5
6
7
$ gradle init --type <name>
  <name>
 - java-application
 - java-library
 - scala-library
 - groovy-library
 - basic


2. Add local jar files for offline build

- Add jar files to the dependencies category in the root build.gradle file.
1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
 plugins {
  id 'java'
  id 'application'
 }

 mainClassName = 'com.app.App1'
  
 dependencies {
  compile files('libs/commons-io-2.5.jar')
  compile files('libs/logback-classic-1.2.3.jar')
  compile files('libs/logback-core-1.2.3.jar')
  compile files('libs/slf4j-api-1.7.25.jar')
 }

 repositories {
  jcenter()
 }


3. Make multiple start script

- Add below command in the root build.gradle file

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
 applicationName = 'App1'
 applicationDefaultJvmArgs = ["-Xms512m", "-Xmx1024m"]

 task createApp2Script(type: CreateStartScripts) {
  mainClassName = "com.app.App2"
  classpath = startScripts.classpath
  outputDir = startScripts.outputDir
  applicationName = 'App2'
  defaultJvmOpts = ["-Xms1024m", "-Xmx2048m"]
 }

 applicationDistribution.into("bin") {
  duplicatesStrategy= DuplicatesStrategy.EXCLUDE
  from(createApp2Script)
  fileMode = 0755
 }
 distZip {
  archiveName 'AppRelease.zip'
 }



4. Make single combined jar files 

- Add below command in the root build.gradle file

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 task makeApp1Jar(type: Jar) {
  manifest {
   attributes 'Implementation-Title': 'AppRelease',  
    'Implementation-Version': '1.0.0',
    'Main-Class': 'com.app.App1'
  }
  baseName = project.name + '_App1'
  from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
  with jar
 }
 task makeApp2Jar(type: Jar) {
  manifest {
   attributes 'Implementation-Title': 'AppRelease',  
    'Implementation-Version': '1.0.0',
    'Main-Class': 'com.app.App2'
  }
  baseName = project.name + '_App2'
  from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
  with jar
 }
 applicationDistribution.into("bin") {
  duplicatesStrategy= DuplicatesStrategy.EXCLUDE
  from(makeApp1Jar)
  from(makeApp2Jar)
  from(createApp2Script)
  fileMode = 0755
 }








댓글 없음:

댓글 쓰기