Barebones Gradle build from lib dir - groovy

I'm struggling to configure a super simple (my 1st ever) Gradle buildscript, that will:
Compile all the Groovy code under my src/main/groovy dir; compilation needs to include all the local (not from a repo) JARs stored in my lib/ directory
Place that compiled code under /bin (or wherever, I really don't care)
JAR up the compiled code into myapp.jar
Somehow include the wrapper task so that the gradlew gets generated as is appropriate
My project dir structure:
myapp/
src/main/groovy/
<Groovy sources>
bin/
lib/
<lots of JARs>
build.gradle
gradle.properties
So far this is what I've tried:
apply groovy
task compile {
println "Compiling from src/main/groovy and lib/ to bin/"
javac ???
}
task jar {
println "JARring up the code"
jar ???
}
Any help or nudges in the right direction would be enormously helpful for me.

What You need to do is just to apply plugin: 'groovy' and add the following section:
dependencies {
compile fileTree(dir: 'lib', include: '*.jar')
}

Related

Download and commit gradle dependencies and plugins in Android Studio

This is an excerpt from a build.gradle file for one of my modules. I'm using android-studio-1.5.1 and gradle 2.10.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:design:23.2.1'
compile 'com.google.android.gms:play-services-gcm:9.0.0'
compile 'com.squareup.picasso:picasso:2.5.2'
}
I also have classpath 'com.google.gms:google-services:3.0.0' in the project level build.gradle file.
I'm trying to gather all the associated jars into a directory which I can commit to my git repo. Something like:
task copyRuntimeLibs(type: Copy) {
into "${projectDir}/libs"
from configurations.compile
}
(This does not work)
Also, I'm not trying to download the sources or javadocs.
I need to be able to commit all dependencies so that the project can be shared on an intranet without internet access.
I've written a plugin which will download all jars and poms. See the code here to download all jars and all poms from a Gradle Configuration
Note: There's a failing test here which shows that the parent pom's are not being downloaded. I've raised an issue here on Gradle's github.
I will likely improve the plugin to invoke the Maven Model Builder APIs to get the parent poms.
FYI - I've already integrated the ModelBuilder APIs successfully with Gradle (see here and here) so shouldn't be too difficult.

Where do i add my dependencies? In which build.gradle to put them?

I am requested to add a few dependencies. I know They should be added on build.gradle, but in the dependencies section is written:
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
I am new to android so from my small experience and googling there are supposed to be two build.gradle files, and only in one of them I should add dependencies, but I can not find an extra build.gradle file!?
I will be happy for help! Where should I add my dependencies and where did my second build.gradle disappear?
Gradle is a bit of an odd tool.
https://docs.gradle.org/current/userguide/artifact_dependencies_tutorial.html
They state here how it works. There is only one build.gradle per project that will pull and manage dependencies for you.
Android Studio extends this. There is one 'main' build.gradle for the entire project, and then for each submodule there is a build.gradle since they are run as separate programs. in the master project build.gradle, put dependencies that effect everything you are doing in the build process, and then for each module dependencies specific for those modules. That's what it's saying.
http://developer.android.com/tools/building/configuring-gradle.html
**EDIT: **
Android Studio docs:
Declare dependencies
The app module in this example declares three dependencies:
dependencies {
// Module dependency
compile project(":lib")
// Remote binary dependency
compile 'com.android.support:appcompat-v7:19.0.1'
// Local binary dependency
compile fileTree(dir: 'libs', include: ['*.jar']) }
Each of these dependencies is described below. The build system adds all the compile
dependencies to the compilation classpath and includes them in the
final package.
Gradle docs:
Example 7.1. Declaring dependencies
build.gradle
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final'
testCompile group: 'junit', name: 'junit', version: '4.+'
}
Dependencies can be listed in a bunch of different ways.

Gradle - build jar which includes both .class and .java files?

I'm using android studio + gradle. I have a module that is a plain standalone java app. Is there a way I can package that module into a .jar file that includes both its compiled .class files, and its source .java files? By default it looks like android studio is only including .class files for me.
I was originally doing this using Eclipse (export as a jar), but can't figure out how to do the equivalent with android studio.
---- EDIT -----
This is my current build.gradle file, but it still outputs a jar that only includes the .class files:
apply plugin: 'java'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
task sourcesJar(type: Jar) {
from sourceSets.main.java.srcDirs
classifier = 'sources'
}
artifacts {
archives sourcesJar
}
Thanks
It is most common to publish a separate sources JAR from compiled classes.
To do that in Gradle, add this to your Gradle build:
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}
artifacts {
archives sourcesJar
}

Android Studio: including AAR library from a library project

In my Android Studio project I have two subprojects/modules: an Android application (App1) and an Android library project (LibraryProject1). App1 depends on LibraryProject1. So far so good.
However, LibraryProject1, in turn, needs to import an AAR library to work properly.
So my Configuration is as follows:
App1 includes LibraryProject1
LibraryProject1 includes dependency.aar
Now, to include dependecy.aar I use the method detailed here:
How to manually include external aar package using new Gradle Android Build System
So basically in my build.gradle for LibraryProject1 I have:
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
compile (name:'dependency', ext:'aar') //my AAR dependency
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.1'
}
Obviously, I put my dependency.aar file in the libs directory of LibraryProject1
However, this doesn't work. It seems that the repository added by LibraryProject1 is completely ignored and the local "libs" folder is not included as a repository, causing compilation to fail.
If I add the repository from the App1's build.gradle it works, but I don't want to do that, it's LibraryProject1 that needs the AAR file, not App1.
How can I do this??
Well, I found a way, but since it's very "hacky" I'll leave the question open, in case anyone comes up with a better, "proper" solution.
Basically the problem is that the flatDir repository is ignored at compilation time if included from LibraryProject1's build.gradle script, so what I do is I use App1's build.gradle to "inject" the flatDir repository in LibraryProject1. Something like this:
//App1 build.gradle
dependencies {
//get libraryproject1 Project object
Project p = project(':libraryproject1')
//inject repository
repositories{
flatDir {
dirs p.projectDir.absolutePath + '/libs'
}
}
//include libraryproject1
compile p
}
This effectively allows LibraryProject1 to include the external AAR library without having App1 include it. It's hacky but it works. Note that you still have to put:
//LibraryProject1 build.gradle
repositories {
flatDir {
dirs './libs'
}
}
inside LibraryProject1's build.gradle otherwise, even if the project itself would compile fine, the IDE wouldn't recognize the types included in the AAR library. Note that the ./ in the path also seems to be important, without it the IDE still doesn't recognized the types.
I faced to the same issue, and I figure out it by putting all libraries on that depends LibraryProject1 in LibraryProject1/libs as a .jar.
I think that aar library cannot be linked to another aar library.
Hope that help you,
Best regards

Access to variables in root gradle build.gradle from subproject build.gradle?

I have a root build.gradle that contains some variables I want to have as global variables for some subprojects but not all sub projects
I created a list:
List spring_dependencies = [
"org.springframework:spring-beans:$springVersion",
"org.springframework:spring-core:$springVersion",
"org.springframework:spring-web:$springVersion",
]
I have some suprojects that do NOT use this for compilation, so I want to only add:
compile spring_dependencies
to the projects that actually need spring.
How can I accomplish this global variable sharing in gradle?
One method I have just tried (and it seems to work) is to declare another sub-project (I called it 'spring'), which has the following build.gradle:
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile "org.springframework:spring-beans:$springVersion"
compile "org.springframework:spring-core:$springVersion"
compile "org.springframework:spring-web:$springVersion"
}
Add this project in to the settings.gradle list of sub-projects, then in the build.gradle that requires the spring libraries, you can do:
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile project(':spring')
}
To extract the dependencies from your spring sub-project.
There might be a better way to achieve the same result... :-/

Resources