0

I include ProjectA in ProjectB with compile files('../ProjectA/build/libs/ProjectA-1.0-SNAPSHOT.jar'). However, when running ProjectB I get classnotfound errors for dependencies in ProjectA. Like the selenium webdriver and okhttp. What must I do to get past those errors?

ProjectA build.gradle

plugins {
    id 'java'
    id 'org.jetbrains.kotlin.jvm' version '1.3.61'
    id 'application'
}

group 'com.company.projectA'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

sourceSets {
    main.java.srcDirs = ['src']
//    main.kotlin.srcDirs = ['src/main/java', 'src/main/kotlin']
    main.kotlin.srcDirs = ['src']
    main.resources.srcDirs = ['src/main/resources']
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
    compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '2.41.0'
    implementation("com.squareup.okhttp3:okhttp:4.3.1")

}

Project B build.gradle

plugins {
    id 'java'
    id 'org.jetbrains.kotlin.jvm' version '1.3.61'
    id 'application'
}

group 'com.company.projectB'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

sourceSets {
    main.java.srcDirs = ['src']
    main.kotlin.srcDirs = ['src']
    main.resources.srcDirs = ['src/main/resources']
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"

    // This pulls in the local project, but leaves out its dependencies.
    compile files('../ProjectA/build/libs/ProjectA-1.0-SNAPSHOT.jar')
}
BAR
  • 15,909
  • 27
  • 97
  • 185

1 Answers1

1

Depending on the generated jar file adds only the compiled classes from ProjectA as the dependency. In this case ProjectB should depend on ProjectA itself - that will add also ProjectA dependencies as transient dependencies.

Put settings.gradle in root directory of both projects:

settings.gradle
ProjectA/
  build.gradle
ProjectB/
  build.gradle

Include both projects in settings.gradle:

include ':ProjectA', ':ProjectB'

Add ProjectA as a dependency in ProjectB/build.gradle:

dependencies {
  compile project(':ProjectA')
}

Or as in the example Project B build.gradle:

plugins {
    id 'java'
    id 'org.jetbrains.kotlin.jvm' version '1.3.61'
    id 'application'
}

group 'com.company.projectB'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

sourceSets {
    main.java.srcDirs = ['src']
    main.kotlin.srcDirs = ['src']
    main.resources.srcDirs = ['src/main/resources']
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"

    // This adds the local project with all its transient dependencies
    implementation project(':ProjectA')
}
Valts Mazurs
  • 81
  • 1
  • 3
  • Can you show it as it would be in my example files? – BAR Feb 20 '20 at 19:12
  • I edited the answer to include the modified original Project B build.gradle. Please let me know if it helps. – Valts Mazurs Feb 20 '20 at 20:10
  • ProjectA and ProjectB are totally separate, it wouldn't make sense to put a top level settings.gradle. – BAR Feb 20 '20 at 21:45
  • In that case it would make more sense to publish ProjectA to a Maven repository (it could be even local repository) and add it into ProjectB as a regular dependency - same way as you added Kotlin Stdlib. When you want to use the local ProjectA instead of the published artifact then make use of Gradle dependency substitution via composite build by adding "includeBuild '../ProjectA'" in ProjectB settings.gradle file. Publishing to Maven: https://docs.gradle.org/current/userguide/publishing_maven.html Composite builds: https://docs.gradle.org/current/userguide/composite_builds.html – Valts Mazurs Feb 21 '20 at 09:12