简体   繁体   English

使用Gradle包含另一个项目?

[英]Include another project using Gradle?

I have the following directory structure: 我有以下目录结构:

dir
 |
 |---project1
 |     |
 |     |--build.gradle
 |     |--src/main/java/com/bpd/app
 |                               |
 |                               |--SomeClass.java
 |
 |---project2
       |
       |--build.gradle
       |--src/main/java/com/bpd/app
                                 |
                                 |--AnotherClass.java
                                 |--MyClass.java

Both are separate Java projects, but SomeClass will not compile because it cannot find MyClass . 两者都是单独的Java项目,但SomeClass不会编译,因为它找不到MyClass

I can add project2 to the build path for project1 using Eclipse (and SomeClass will then compile), but I want to know how to do it using Gradle. 我可以使用Eclipse将project2添加到project1的构建路径(然后SomeClass将编译),但我想知道如何使用Gradle来完成它。 Is it possible? 可能吗? How can I make Gradle add project2 to the build path for project1 ? 如何让Gradle将project2添加到project1的构建路径?

Here's the build.gradle file for one of the projects. 这是其中一个项目的build.gradle文件。 Only the library dependencies differ. 只有库依赖项不同。

apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'war'

apply plugin: 'checkstyle'
apply plugin: 'pmd'
apply plugin: 'findbugs'
apply plugin: 'jacoco'

repositories {
  mavenCentral()
}

dependencies {
  compile "javax.ws.rs:jsr311-api:1.1.1"

  compile 'com.sun.jersey:jersey-server:1.13'
  compile 'com.sun.jersey:jersey-core:1.13'
  compile 'com.sun.jersey:jersey-servlet:1.13'
  compile 'com.sun.jersey:jersey-json:1.13'

  compile 'javax.ejb:ejb-api:3.0'

  compile 'org.eclipse.persistence:javax.persistence:2.0.0'
  compile 'org.eclipse.persistence:org.eclipse.persistence.jpa:2.6.2'
  compile 'org.xerial:sqlite-jdbc:3.8.11.2'

  testCompile 'junit:junit:4.12'
  testCompile 'org.assertj:assertj-core:3.2.0'
}

sourceSets.test.resources {
  srcDirs = ["src/test/java", "src/test/resources"]
  exclude "**/package-info.java"
}

jar {
  from configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}

tasks.withType(FindBugs) {
    reports {
        xml.enabled = false
        html.enabled = true
    }
}

checkstyle {
  toolVersion = "6.13"
}

Its quite simple to do. 它很简单。

Step 1 步骤1

Just add these lines to your settings.gradle file 只需将这些行添加到settings.gradle文件即可

include ':library'
project(':library').projectDir = new File("../MyLibrary/library")

The first line simply includes the library and the second line points to the project directory. 第一行只包含库,第二行指向项目目录。 So be sure to do it correctly. 所以一定要正确地做到这一点。 The ".." is simply to navigate a directory back. “..”只是简单地导航目录。

Step 2 第2步

Just add this line to the build.gradle of your application. 只需将此行添加到应用程序的build.gradle即可。

compile project(':library')

This line compiles the project with your application. 该行用您的应用程序编译项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM