How to configure settings.gradle for multiproject? - gradle

How should I structure my build.gradle and settings.gradle to include another project with own build and settings gradle files? Currently I get:
Configuration with name 'default' not found.
My settings.gradle:
include :sliding-layer'
project(':sliding-layer').projectDir = new File(rootDir, 'modules/sliding-layer:Library')
and build.gradle:
compile project(':modules:sliding-layer')
Second project settings.gradle:
include ':Library'
include ':SlidingLayerSample'
and build.gradle (in root dir):
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
}
}
allprojects {
group = 'com.6wunderkinder.slidinglayerlibrary'
version = '1.1-SNAPSHOT'
repositories {
mavenCentral()
}
}
apply plugin: 'android-reporting'

You can only have one settings.gradle per build, but you can refer to nested projects in the top-level settings.gradle:
include ':sliding-layer:Library'
include ':sliding-layer:SlidingLayerSample'

Related

Add dependency and plugin in seperate file using "apply from" in gradle

My build.gradle inside app directory contains:
apply plugin: 'com.android.application'
buildscript {
repositories {
mavenCentral()
jcenter()
maven {
url "https://maven.google.com"
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
}
}
allprojects {
repositories {
mavenCentral();
jcenter()
}
}
def hasBuildExtras = file('build-extras.gradle').exists()
if (hasBuildExtras) {
apply from: 'build-extras.gradle'
}
And this is my build-extras.gradle file:
buildscript {
repositories {
mavenCentral()
jcenter()
maven {
url "https://maven.google.com"
}
}
dependencies {
classpath 'com.github.triplet.gradle:play-publisher:1.2.2'
}
}
apply plugin: 'com.github.triplet.play'
Am I correct to assume that build-extras.gradle should be "merged" inside original file? No matter how I move it around I get Error:Plugin with id 'com.github.triplet.play' not found.
If i move classpath 'com.github.triplet.gradle:play-publisher:1.2.2' up to the main file and leave just apply plugin: 'com.github.triplet.play' inside build-extras.gradle then it seems to work fine. So am I wrongly defining dependencies?
A build.gradle file can only ever have one buildscript block. You are correct with the paragraph explanation. Add the plugin dependency to your main build file's dependencies block within the buildscript, then conditionally apply the plugin using whatever logic you'd like.

Gradle: package multi-project into a single jar

I have a gradle multi-project and want to create a single jar (library) containing all the classes of my subprojects and external dependencies.
I have the following project structure. Each project has its own 3rd party dependencies. Common dependencies are included in the root project. The two modules A and B are dependent on the core.
+ root-project (only build.gradle and settings.gradle)
- core (src/main/java, src/main/resources, ..)
- module-A (src/main/java, src/main/resources, ..)
- module-B (src/main/java, src/main/resources, ..)
To export a single jar, I added the following task to the build.gradle of the root project:
apply plugin: "java"
subprojects.each { subproject -> evaluationDependsOn(subproject.path)}
task allJar(type: Jar, dependsOn: subprojects.jar) {
baseName = 'multiproject-test'
subprojects.each { subproject ->
from subproject.configurations.archives.allArtifacts.files.collect {
zipTree(it)
}
}
}
artifacts {
archives allJar
}
This approach works, but does only collect the project source files. The 3rd party dependencies are ignored. So I tried out the Shadow Plugin (http://imperceptiblethoughts.com/shadow/) which should also include external dependencies.
Unfortunately the plugin does not collect anything at all. This is most probably due to missing dependencies between the root project and its sub projects. How can I tell the shadow plugin, that it should collect the sources of the subprojects? Or is there a better approach to export a single library out of multiple projects?
complete build.gradle using the shadow plugin:
/****************************************
* instructions for all projects
****************************************/
allprojects {
apply plugin: 'idea'
apply plugin: 'eclipse'
group = 'com.test.multi-project'
version = '1.0'
}
/****************************************
* instructions for each sub project
****************************************/
subprojects {
apply plugin: "java"
sourceCompatibility = 1.9
targetCompatibility = 1.9
repositories {
mavenCentral()
}
dependencies {
compile "org.slf4j:slf4j-api:1+"
compile "ch.qos.logback:logback-core:1+"
compile "ch.qos.logback:logback-classic:1+"
testCompile "junit:junit:4+"
}
}
/****************************************
* Single jar out of all sub projects
****************************************/
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.2'
}
}
apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'java'
shadowJar {
baseName = 'multiproject-test'
}
The submodules are included in the settings.gradle of the root project
rootProject.name = 'myproject-root'
// submodules
include ":core"
include ":module-A"
include ":module-B"
Thanks for your help!
I solve my problem with the solution explained here: https://discuss.gradle.org/t/how-to-get-gradle-install-to-actually-bundle-all-project-subproject-classes-resources-etc/12070/4
My build.gradle looks now like this:
/****************************************
* instructions for all projects
****************************************/
allprojects {
apply plugin: 'idea'
apply plugin: 'java'
repositories {
mavenCentral()
}
group = 'com.test.multiproject'
version = '1.0'
sourceCompatibility = 1.9
targetCompatibility = 1.9
}
/****************************************
* instructions for each sub project
****************************************/
subprojects {
// common dependencies
dependencies {
compile "org.slf4j:slf4j-api:1+"
compile "ch.qos.logback:logback-core:1+"
compile "ch.qos.logback:logback-classic:1+"
testCompile "junit:junit:4+"
}
}
/****************************************
* Single library jar containing all sub projects and 3rd party dependencies
****************************************/
configurations {
childJars
}
dependencies {
subprojects.each {
childJars project(it.path)
}
}
jar {
dependsOn configurations.childJars
from { configurations.childJars.collect { zipTree(it) } }
}
How about getting all runtime libs while building jar itself
jar {
archiveName 'Some.jar'
manifest {
attributes 'Implementation-Title': 'Some',
'Plugin-Class': 'main'
}
from {configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it)}}
}
What about something simple like that :
task fatJar(type: Jar) {
subprojects.each { subproject ->
from subproject.configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
}

Plugin with id 'com.wonbin.myplugin' not found

I wrote a demo for learning gradle for android . i used the JAR
1) project root dir build.gradle
buildscript {
repositories {
jcenter()
flatDir {dirs 'build_libs'}
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'com.wonbin.myplugin:MyPlugin:1.0'
}
}
apply plugin: 'com.wonbin.myplugin'
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
I have leanrd that the plugin id is the properties filename in the plugin project. But still when I run ./gradlew assemble , it happens:
Build file '/home/wonbin/MyApp/build.gradle' line: 13
What went wrong: A problem occurred evaluating root project 'MyApp'.
Plugin with id 'com.wonbin.myplugin' not found.
what should i do ?
Probably your build_dir doesn't correspond to the correct file structure. You can reference your jar in the classpath directly instead, like this:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath file('/path/to/your/plugin.jar')
}
}
apply plugin: 'com.wonbin.myplugin'

Gradle error: Could not find org.apache.httpcomponents:httpclient-android:4.3.5.1

I'm trying do use the Apache httpclient for Android in my Gradle project and I don't understand why I get this error when running ./gradle build:
Could not find org.apache.httpcomponents:httpclient-android:4.3.5.1.
My top-level build.gradle contains:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
}
}
My project build.gradle starts with:
apply plugin: 'com.android.application'
dependencies {
compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':workspace:google_play_services:libproject:google-play-services_lib')
}
What's wrong with that?
In The Central Repository Browser of Maven the package I need is listed:
http://search.maven.org/#browse|-305040853
You have to add in your project build.gradle
repositories {
mavenCentral()
}
If you would like to put it in the top level file you can add this in the top level build.gradle:
allprojects {
repositories {
mavenCentral()
}
}

Configuration for projects in subdirectory

I have a Gradle multi-project build that looks like this:
rootProject
build.gradle
settings.gradle
shared/
SharedLib
SharedLib2
plugins/
FirstPlugin
SecondPlugin
I'd like to add all projects in the directory shared as dependencies to all projects in plugins.
More generally speaking: How do I configure subprojects by directory?
Contents of settings.gradle:
include 'plugins:FirstPlugin', 'plugins:SecondPlugin', 'shared:SharedLib', 'shared:SharedLib2'
Contents of build.gradle:
task wrapper(type: Wrapper) { gradleVersion = "2.1" }
allprojects{ apply plugin:"java" }
subprojects {
group = "eu.test.myGroup"
repositories { mavenCentral() }
dependencies { testCompile "junit:junit:4.11" }
}
Adding this to build.gradle does what I wanted to achieve:
// Define what's a plugin and what's a shared library by directory paths.
// Ignore empty projects
def plugins = subprojects.findAll{ it.path.contains("plugins") && hasSrc(it) }
def sharedLibs = subprojects.findAll{ it.path.contains("shared") && hasSrc(it)}
/** Checks whether a project has a source directory */
def hasSrc(proj){
return new File(proj.projectDir, "src").exists()
}
// Configure the plugins to depend on the shared libraries at compile time
configure(plugins) { dependencies { sharedLibs.each{compile it} } }

Resources