Gradle Project Build Order For Maven Tasks

2019-09-09 22:56发布

问题:

Consider the following multi-project build script:

build.gradle

subprojects {
  apply plugin: 'java'
  apply plugin: 'maven'
  group = "myorg"
  version = "1.0.0-SNAPSHOT"
}

project(':client') {
  dependencies {
    compile 'myorg:shared:1.0.0-SNAPSHOT'
  }
}

With the following files:

├── build.gradle
├── client
│   └── src
│       └── main
│           └── java
│               └── myorg
│                   └── client
│                       └── MyOrgClient.java
├── settings.gradle
└── shared
    └── src
        └── main
            └── java
                └── myorg
                    └── shared
                        └── MyOrgObj.java

In the above files MyOrgClient.java includes myorg.shared.MyOrgObj and settings.gradle has the single line include 'client', 'shared'

Problem

The project/task build order for maven related tasks like installing locally and deploying to remote repositories does not take into account the implied project dependency. Because gradle does not know that 'myorg:shared:1.0.0-SNAPSHOT' is created by project(':shared'), the build order is :client -> :shared and causes errors like the one below:

$ gradle install
:client:compileJava

FAILURE: Build failed with an exception.

* What went wrong:
Could not resolve all dependencies for configuration ':client:compile'.
> Could not find myorg:shared:1.0.0-SNAPSHOT.
  Required by:
      myorg:client:1.0.0-SNAPSHOT

Question: Is there a standard way to deal with this problem? I have tried these solutions without success:

  • Using mustRunAfter but ran into problems with tasks not existing yet. I also don't think this would scale well with a large number of projects
  • Adding archives project(':shared') to the client's dependencies
  • Adding compile project(':shared') to the client's dependencies and then removing it from the generated pom. Unfortunately this doesn't add the dependency to the install task or artifactoryPublish Edit: This actually was the solution. A project dependency will provide the correct version/name/group in the generated pom.xml so the explicit group:name:version dependency is not needed

回答1:

You have to define the dependencies between the projects more or less the same way as in Maven:

For example like this:

project(':app') {
  apply plugin: 'ear'
  dependencies {
    compile project (':webgui')
    compile project (':service')
  }
}

But you need to define the settings.gradle which contains the modules like this:

include 'app'
include 'domain'
include 'service'
include 'service-client'
include 'webgui'


标签: maven gradle