Java class files only compiling in subproject, but not main project


(Chris) #1

Hello, first post here, so sorry if I mess anything up! Ive been reading through the docs and been trying to get two java projects that I had previously, integrated into gradle.

So far I have a project A (main) which is depending on project B (subproject). Currently I am using a flat structure as so:

ProjectA
-->build.gradle
-->settings.gradle
-->src
    -->code
ProjectB
-->build.gradle
-->settings.gradle
-->src
    -->code

Funny thing is, is when I build a sample project from scratch using this system, this method is fine and I get an expected ProjectA jar with class files and a ProjectB jar. But when I try and implement this onto my java projects it doesnt seem to work. ProjectA jar is empty except for the manifest. (However, when I build ProjectB by itself, it does build a jar with the right class files)

Ive searched all over SO and here for something I might have missed when trying to implement gradle onto a java project (id post the links but thered be huge list), maybe theres something I missed about applying gradle to a java project already built? Ive posted the build/settings files for each below

ProjectA
–>build.gradle

apply plugin: 'java'
apply plugin: 'application'

sourceCompatibility = 1.8

repositories {
    jcenter()
}

sourceSets {
   main {
      java {
         srcDir 'src'
      }
   }
	
   test {
      java {
         srcDir 'test'
      }
   }
}

dependencies {
    compile project(':ProjectB') 
    compile fileTree(dir: 'libraries', include: '*.jar')

    // This dependency is found on compile classpath of this component and consumers.
    compile 'com.google.guava:guava:20.0'
    // Use JUnit test framework
    testCompile 'junit:junit:4.12'
}

jar {
    def manifestClasspath = configurations.runtime.collect { it.getName() }.join(' ')
    manifest {
        attributes  'Implementation-Title': "Bar thing",
                    'Implementation-Version': '0.0.1',
                    'Built-By': System.getProperty('user.name'),
                    'Built-Date': new Date(),
                    'Built-JDK': System.getProperty('java.version'),
                    'Built-Gradle': gradle.gradleVersion,
                    'Class-Path': manifestClasspath,
			'Main-Class': 'gui.ProjectA'
    }
}

–>settings.gradle

includeFlat "ProjectB"
rootProject.name = 'ProjectA'

ProjectB
–>build.gradle

apply plugin: 'java-library'
repositories {
    jcenter()
}

sourceSets {
   main {
      java {
         srcDir 'src'
      }
   }
	
   test {
      java {
         srcDir 'test'
      }
   }
}

dependencies {
    compile fileTree(dir: 'libraries', include: '*.jar')
	
    // This dependency is exported to consumers, that is to say found on their compile classpath.
    api 'org.apache.commons:commons-math3:3.6.1'
    // This dependency is used internally, and not exposed to consumers on their own compile classpath.
    compile 'com.google.guava:guava:20.0'	
    // Use JUnit test framework
    testImplementation 'junit:junit:4.12'
}

jar {
    def manifestClasspath = configurations.runtime.collect { it.getName() }.join(' ')
    manifest {
        attributes  'Implementation-Title': "Foo thing",
                    'Implementation-Version': '0.0.1',
                    'Built-By': System.getProperty('user.name'),
                    'Built-Date': new Date(),
                    'Built-JDK': System.getProperty('java.version'),
                    'Built-Gradle': gradle.gradleVersion,
                    'Class-Path': manifestClasspath
    }
}

–>settings.gradle

rootProject.name = 'ProjectB'

(uklance) #2

Gradle projects only have a single settings.gradle


(Chris) #3

Each project only has 1 settings file


(uklance) #4

Is this two separate builds? or one multi-module build?
If one multi-module build then there should be one settings.gradle for the two modules


(Chris) #5

I believe it is multi-module, ProjectA depends on ProjectB. I understood that both projects had to have a settings file however. I created a separate demo project mocking that layout fine and it worked so I thought I could just transpose that onto my actual project


(uklance) #6

A multi-module build has a single settings.gradle in the root project and all submodules must be declared there. Any other settings.gradle will be ignored.


(Chris) #7

Okay thanks, however, only my subproject is compiling into class files, my main project is still empty for some reason and does not seem to be compiling even though when calling the build command it says its successful