Build empty jar file first time on Centos 7


(Ma) #1

Hi All,

Im using Gradle 4.0 on Linux OS(Centos7), when i use my gradle script(build.gradle) to build jar file first time on Centos 7, it doesnt work only generate an empty jar file which doesnt have class file in jar, but when i repeat send same command to build jar file it works and the jar file doesnt empty again, how can i fix this issues?

my build jar file task was ‘release’ and the build.gradle are as fellows:
apply plugin: 'java’
apply plugin: ‘jacoco’
sourceCompatibility = 1.7
targetCompatibility = 1.7
compileJava.options.encoding = 'UTF-8’
version = ‘1.0’

sourceSets {
main {
resources {
srcDir ‘src/main/java’
}
}
}

repositories {
mavenCentral()
}

jacoco {
toolVersion = “0.7.6.201602180812"
reportsDir = file(”$buildDir/customJacocoReportDir")
}

dependencies {
testCompile group: ‘junit’, name: ‘junit’, version: ‘4.+‘
compile fileTree(dir: ‘lib’, include: [’*.jar’])
}

task copyJars(type: Copy) {
from 'lib/'
into 'target/lib/'
it.setOnlyIf { true }
it.outputs.upToDateWhen { false }
}

task copyConfigureFiles(type: Copy){
from 'config’
into 'target/config/'
it.setOnlyIf { true }
it.outputs.upToDateWhen { false }
}

File classFolder =file(‘build/classes/java/main/’)

task taskJar(type:Jar, dependsOn: [copyJars,copyConfigureFiles,compileJava,compileTestJava]) {

onlyIf { !sourceSets.main.allSource.files.isEmpty() }

baseName 'meu_addition'

if(classFolder.exists()){

	from classFolder
	include "/xxx/xxx/xxx/xxx/xxx/*.class"
}else{
	
	from 'build/classes/main/'
	include "/xxx/xxx/xxx/xxx/xxx/*.class"
}

manifest {
    attributes 'Implementation-Title': 'Gradle Jar File Example',
    		   'Implementation-Version': version,
    		   'Main-Class': 'xxx.xxx.xxx.xxx.xxx.Addition'
    attributes 'Class-Path': 'config/ ' + configurations.compile.collect {'lib/' + it.getName()}.join(' ')
    
}

it.setOnlyIf { true }
it.outputs.upToDateWhen { false }

}

task release(type: Copy,dependsOn: taskJar) {
from(‘build/libs’) {
include ‘*.jar’
}
into (‘target’)

it.setOnlyIf { true }
it.outputs.upToDateWhen { false }

}


(Carlo Luib-Finetti) #2

I think your sourceSets definition is just wrong.

First of all: if you’re using a standard Java layout of source and resource folders, you do not have declare sourceSets - Gradle prefers standard “conventions over configuration”. Do not complicate things with unneeded stuff.

Then have a look at the manual at chapter 47.4.1. Changing the project layout, where you can see the simple example for sourceSets definitions.


(Ma) #3

Hi, thank you for your help, finally i solved this problem, and the reason as bellow:

  1. java sourceSets wasn`t right
  2. i use gradle3.5 version on window and use gradle4.0 in linux, so i need to judge the classes folder, so i change all the OS use gradle 4.0 and remove those judgement then the issues disappeared !
  • File classFolder =file(‘build/classes/java/main/’)

  • if(classFolder.exists()){

  • from classFolder

  • include “/xxx/xxx/xxx/xxx/xxx/*.class”

  • }else{

  • from ‘build/classes/main/’

  • include “/xxx/xxx/xxx/xxx/xxx/*.class”

  • }


(Carlo Luib-Finetti) #4

Maybe I do not understand what you trying to do. But if you just want to compile and have a jar as result, you simply have to do - nothing! Just call “gradle jar” and Gradle knows how to produce the jar.
Remember: “Convention over Configuration” - if you follow standards on laying out the files a la “src/main/java/xxx” and “src/main/resources” you’re done.