Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
712 views
in Technique[技术] by (71.8m points)

Gradle Project Build Order For Maven Tasks

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...