Gradle subproject not included in classpath

后端 未结 2 1788
一向
一向 2021-02-08 03:16

We have a set up with 2 project, 1 main and 1 subproject, they are Java projects. They are all under the same directory.

Here is how the directory structure looks like :

相关标签:
2条回答
  • 2021-02-08 03:25

    Based on the Java Quckstart: Multi-project Java build:

    1) your settings.gradle file needs to be in dev/, not dev/project_A and it should contain something like this:

    include 'Project_A', 'Project_B'
    

    2) Then your dev/Project_A/build.gradle file should contain

    dependencies {
        compile project(':Project_B')
    }
    

    Edit:

    I have created a toy example following the project layout you've described in your question. However, I haven't been able to reproduce the problem. Perhaps you'll be able to spot some difference that is causing your particular error:

    Directory Tree

    ├── Project_A
    │   ├── build.gradle
    │   ├── settings.gradle
    │   └── src
    │       └── main
    │           └── java
    │               └── a
    │                   └── A.java
    └── Project_B
        ├── build.gradle
        └── src
            └── main
                └── java
                    └── b
                        └── B.java
    

    Project_A/build.gradle

    apply plugin: 'java'
    apply plugin: 'application'
    
    mainClassName = 'a.A'
    
    dependencies {
        compile project(':Project_B')
    }
    

    Project_A/settings.gradle

    includeFlat 'Project_B'
    

    A.java

    package a;
    import b.B;
    
    public class A {
        public static void main(String[] args) {
            System.out.println("From A.main...");
            B.call();
        }
    }
    

    Project_B/build.gradle

    apply plugin: 'java'
    

    B.java

    package b;
    
    public class B {
        public static void call() {
            System.out.println("Calling B");
        }
    }
    

    When running Gradle from Project_A, the output is:

    $ gradle clean build run
    :clean UP-TO-DATE
    :Project_B:clean UP-TO-DATE
    :Project_B:compileJava
    :Project_B:processResources UP-TO-DATE
    :Project_B:classes
    :Project_B:jar
    :compileJava
    :processResources UP-TO-DATE
    :classes
    :jar
    :assemble
    :compileTestJava UP-TO-DATE
    :processTestResources UP-TO-DATE
    :testClasses UP-TO-DATE
    :test UP-TO-DATE
    :check UP-TO-DATE
    :build
    :Project_B:assemble
    :Project_B:compileTestJava UP-TO-DATE
    :Project_B:processTestResources UP-TO-DATE
    :Project_B:testClasses UP-TO-DATE
    :Project_B:test UP-TO-DATE
    :Project_B:check UP-TO-DATE
    :Project_B:build
    :run
    From A.main...
    Calling B
    
    BUILD SUCCESSFUL
    
    0 讨论(0)
  • 2021-02-08 03:31

    Ok, in case this is useful to someone, I finally got this working with actually removing (git rm) the .claspath file. It was in the .gitignore, but for some reason, it seems it had been comitted at some point and was playing around.

    After doing this and re-importing the projects in Eclipse everything went back to normal.

    Thanks to the ones who tried to help.

    0 讨论(0)
提交回复
热议问题