Gradle dependencies for non multi project environment - eclipse

After some research, I was able to create a non multi project with two projects. Sounds strange? Isn't. It's normal that you don't put all your projects under one root project.
Reading a lot about multi project builds I wondered, why it is so complicated to use one project with another that are not in the same root project.
The only way, that worked for me so fare, is to use Maven publishing.
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'maven-publish'
group = 'foo'
version = '0.2.1'
repositories {
mavenCentral()
mavenLocal()
}
jar { baseName = "${project.group}.${project.name}" }
publishing {
publications {
maven(MavenPublication) {
artifactId "${project.name}"
artifact sourceJar { classifier "sources" }
from components.java
}
}
}
task sourceJar(type: Jar) { from sourceSets.main.allJava }
task wrapper(type: Wrapper) { gradleVersion = '1.11' }
Using: gradlew clean build publishToMavenLocal creates two jar libraries (binary and source) at the correct location in the local maven repository .m2
The second project, that depends on the first one has this build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
group = 'foo2'
version = '0.1'
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
compile("foo:Gradle.test2:0.+")
}
jar { baseName = "${project.group}.${project.name}" }
task wrapper(type: Wrapper) { gradleVersion = '1.11' }
Worked for me. After rebuilding the dependencies I always have the newest version.
Now why do I need the Maven workaround when using Gradle? Can't I publish to the local Gradle cache? What Alternatives are available?

Creating a project dependency without using a common root project is easy once you know how:
In the second project (ProjectTwo), that depends on the first project (ProjectOne), put the following in your settings.gradle file:
include ':ProjectOne'
project(':ProjectOne').projectDir = new File(settingsDir, '../ProjectOne')
The second line above assumes that ProjectOne shares the same parent directory as ProjectTwo, but you can use any relative path to set the correct directory location. Now you can add ProjectOne as a dependency in ProjectTwo's build.gradle file:
dependencies {
compile project(':ProjectOne')
}

Related

How do I run a scala application using gradle?

I added the scala plugin to gradle, but i don't how to run it. There's no run task when i create a scala project.
How do I run the scala project?
My gradle build script:
apply plugin: 'idea'
apply plugin: 'scala'
repositories {
mavenLocal()
maven {
url "http://maven.aliyun.com/nexus/content/groups/public/"
}
mavenCentral()
}
dependencies {
compile group: 'org.scala-lang', name: 'scala-library', version: '2.13.1'
}
You need to provide the classpath as well. Change run task declaration to:
task run(type: JavaExec, dependsOn: classes) {
main = 'Demo'
classpath = sourceSets.main.runtimeClasspath
}
And it will work fine. Demo.

Gradle: add .jar to /war/WEB-INF/lib

I'm having difficulty getting Gradle to add a dependency to my /war/WEB-INF/lib directory in a Google Web Application Project.
apply plugin: 'java'
apply plugin: 'war'
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.code.gson:gson:2.2.4'
}
Using Eclipse, gson-2.2.4.jar shows up in Project and External Dependencies (project compiles) but doesn't get added to /war/WEB-INF/lib (NoClassDef exception at runtime since it can't find the .jar)
I've tried changing webAppDir, webAppDirName, and editing the war task all to no avail.

Gradle custom plugin's build throws "unable to resolve class"

I'm trying to build a gradle custom plugin that in turn depends on other plugin. In particular, the plugin depends on com.bmuschko.docker-remote-api plugin (that again depends on java library com.github.docker-java:docker-java:2.1.1).
So I tried with the following gradle.build file
apply plugin: 'groovy'
apply plugin: 'maven'
apply plugin: 'com.bmuschko.docker-remote-api'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.bmuschko:gradle-docker-plugin:2.6.1'
}
}
group = 'com.example'
version = '1.0'
dependencies {
compile gradleApi()
compile localGroovy()
compile group: 'com.github.docker-java', name: 'docker-java', version: '2.1.1'
}
and the following plugin file:
package com.example.build
import org.gradle.api.Project
import org.gradle.api.Plugin
import com.bmuschko.gradle.docker.tasks.image.DockerBuildImage
class BndPlugin implements Plugin<Project> {
#Override
void apply(Project project) {
task buildDockerImage(type: DockerBuildImage) {
println file("${projectDir}/docker/")
}
}
}
but what I get with gradle build is just the error
unable to resolve class com.bmuschko.gradle.docker.tasks.image.DockerBuildImage
Question: how to properly manage custom plugin's dependencies?
You can get the full plugin project on github.
If you're going to use classes from the plugin, as opposed to just applying it, you should also include the plugin binaries as compile dependency in your lower dependencies section.

Gradle Eclipse not working

My build.gradle script:
apply plugin: 'groovy'
apply plugin: 'eclipse'
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
dependencies {
compile (
'org.apache.camel:camel-core:2.14.1',
'org.apache.camel:camel-mail:2.14.1'
)
}
When I run gradle eclipse I see:
:myapp:eclipseClasspath
:myapp:eclipseJdt
:myapp:eclipseProject
:myapp:eclipse
BUILD SUCCESSFUL
Total time: 3.694 secs
When I run gradle clean build I get a similar BUILD SUCCESSFUL message. But when I refresh my project in Eclipse, I don't see a Referenced Libraries folder with Camel Core or Camel Mail in it, instead under the Problems tab I see 3 problems:
Project 'myapp' is missing required library: 'D:\workspace\myapp\unresolved dependency - org.apache.camel camel-core 2.14.1'
Project 'myapp' is missing required library: 'D:\workspace\myapp\unresolved dependency - org.apache.camel camel-mail 2.14.1'
The project cannot be built until build path errors are resolved
What is going on here? On a perhaps-somewhat-related note, I am on Eclipse Juno, and going into Properties >> Java >> Compiler, I don't seem to have an option to set my Eclipse Java Compiler to 1.8, only 1.7. Perhaps my Eclipse instance is too old to handle Java 8?
If that's your full build script, you're missing repository definitions (where to go get the artifacts). Try adding:
repositories {
mavenCentral()
}
Here's the backing class for repositories {} in case you need to add a custom URL.

Gradle/Eclipse - Add projects to the classpath

In my Eclipse workspace, I have two projects, projectA and projectB. projectA is in a different folder from projectB (separate git repositories, not near each other on the file system). Is there any way for gradle to apply projectB to the classpath of projectA using the gradle plugin for eclipse?
Project A's build.gradle
apply plugin: 'java'
dependencies {
compile 'myorg:projectB:+'
}
Project B's build.gradle
apply plugin: 'java'
publishing {
publications {
ivy(IvyPublication) {
organisation 'myorg'
module 'projectB'
revision '1.0-SNAPSHOT'
descriptor.status = 'integration'
configurations {
archive {}
}
from components.web
}
}
repositories {
ivy {
url "/path/to/repo"
layout "pattern", {
ivy "[organisation]/[module]/[revision]/[module]-[revision].xml"
artifact "[organisation]/[module]/[revision]/[artifact](-[classifier])-[revision].[ext]"
}
}
}
}