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

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.

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: Setting up Scala project with Apache Spark in Eclipse

I am not able to setup a Scala project with Apache Spark dependency in Eclipse. Using a Scala IDE plugin and Gradle plugins in Eclipse. build.gradle project looks like this:
apply plugin: 'scala'
apply plugin: 'eclipse'
repositories{
mavenCentral()
mavenLocal()
}
dependencies{
compile 'org.slf4j:slf4j-api:1.7.5'
compile "org.scala-lang:scala-library:2.11.2"
compile 'com.sparkjava:spark-core:2.3'
testCompile "junit:junit:4.11"
}
task run(type: JavaExec, dependsOn: classes) {
main = 'Main'
classpath sourceSets.main.runtimeClasspath
classpath configurations.runtime
}
Under the Referenced Libraries I can see spark-core-2.3.jar. But I can't import any Spark library into Scala class.
I did try running gradle eclipse command but no luck.
You're referencing the wrong dependency - instead of com.sparkjava:spark-core:2.3 (which belongs to another project, Spark web framework), you should include:
compile 'org.apache.spark:spark-core_2.11:2.0.1'
This uses latest stable version (2.0.1).

Gradle Configuration of pluginRepository

I am trying to get a simple Gradle project (the one that is created by eclipse automatically) with static code analysis made by Sonar to run on our continuous integration. Our CI server is behind a proxy and i have to access the Gradle plugin repository over an internal Nexus server.
As described in the userguide i have added the following to my settings.gradle
pluginRepositories {
maven {
url 'http://link.to.my.nexus'
}
gradlePluginPortal()
}
rootProject.name = 'GradleTestProject'
my build.gradle looks like this:
plugins {
id "org.sonarqube" version "2.0.1"
}
apply plugin: 'java'
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile 'org.slf4j:slf4j-api:1.7.21'
testCompile 'junit:junit:4.12'
}
When i run this on Jenkins, i get the following error message:
FAILURE: Build failed with an exception.
* Where:
Settings file '/opt/hudson/jobs/GradleTestProject/workspace/settings.gradle' line: 1
* What went wrong:
A problem occurred evaluating settings 'workspace'.
> Could not find method pluginRepositories() for arguments [settings_20tc2o9xuj82hi1fvpe4wvcvt$_run_closure1#52b56c40] on settings 'workspace'.
I have looked at other examples in the web. They all do it the same way as i described.
BTW: I am using Gradle 2.12
I'm in version 4.0.1.
I had the same error, found your question without answer and finally I found and tried this in settings.gradle and now it works.
pluginManagement {
repositories {
gradlePluginPortal()
}
}

Running a jar produce from gradle build

I have a simple gradle file from which I build a jar file.
When I run the jar file however I get and error: 'Exception in thread "main" java.lang.NoClassDefFoundError: scala/Predef$'
The build.gradle looks like this:
apply plugin: 'scala'
apply plugin: 'distribution'
def mainClass = "com.domain.Hello"
distributions {
custom {}
}
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.scala-lang', name: 'scala-library', version: '2.11.4'
}
jar {
manifest {
attributes 'Main-Class': mainClass
}
}
task run(type: JavaExec, description: "Runs the project") {
main = mainClass
classpath sourceSets.main.runtimeClasspath
classpath configurations.runtime
}
When you run the jar you need to include the scala-library on the classpath, like this
scala -cp [scala library jar] [project jar]
If you don't want to have to do this, take a look at this blog post http://blogs.steeplesoft.com/posts/2013/09/04/building-fat-jars-with-gradle/
You need to include ''scala-compile' package beside 'scala-library'

Running a gradle jersey 2.0 web app without a web.xml in eclipse

I have a gradle project that I import in eclipse. The project is a web app and I manage to build and deploy it to the tomcat but the jersey REST services are not being loaded and hence I get 404 when trying to reach these services.
I'm using tomcat7 which has support for servlet-api 3.0.
please note that I'm not using a web.xml. I have a class that extends Application and should load the right classes and set the root for the REST services.
Here's my class:
import java.util.Set;
import javax.ws.rs.core.Application;
#javax.ws.rs.ApplicationPath("api")
public class JerseyApplicationConfig extends Application
{
#Override
public Set<Class<?>> getClasses()
{
Set<Class<?>> resources = new java.util.HashSet<Class<?>>();
addRestResourceClasses(resources);
return resources;
}
private void addRestResourceClasses(Set<Class<?>> resources)
{
resources.add(com.cisco.gemini.LoggingRESTService.class);
}
}
The problem is that this class does not get created by jersey and therefore the REST services are not set up.
Thanks,
Gal
Update:
I managed to solve the problem so I'm posting here the solution for everyone's benefit.
Here's my gradle file that got things working. I can now build and run the services on tomcat7 from Eclipse and the jersey servlets load properly.
Note: I also wanted to be able to run the service from command line (for testing purposes) for this reason I applied the jettyEclipse plugin. The jetty plugin that comes with gradle does not support servlet-api 3.0. this isn't working for me yet (because of some conflict on slf4j which I'm using) but it might work for others.
Thanks,
Gal
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'jetty'
apply plugin: 'eclipse-wtp'
repositories {
mavenCentral()
}
buildscript {
repositories {
jcenter()
}
dependencies {
classpath (group: 'com.sahlbach.gradle', name: 'gradle-jetty-eclipse-plugin', version: '1.9.+')
}
}
apply plugin: 'jettyEclipse'
dependencies {
compile 'javax.servlet:javax.servlet-api:3.0.1'
compile 'com.sun.jersey:jersey-server:1.18.1'
compile 'com.sun.jersey:jersey-core:1.18.1'
compile 'com.sun.jersey:jersey-json:1.18.1'
compile 'com.sun.jersey:jersey-servlet:1.18.1'
compile 'javax.ws.rs:jsr311-api:1.1.1'
compile fileTree(dir: 'libs', include: '*.jar')
compile 'org.codehaus.jettison:jettison:1.3.5'
}
eclipse {
wtp {
component {
contextPath = 'MyService'
deployName = 'MyService'
facet {
facet name: 'jst.web', version: '3.0'
facet name: 'java', version: '1.7' }
}
}
}
If you want to make it working from command-line you can try to use gretty plugin - https://github.com/akhikhl/gretty - it supports newer versions of Jetty including Servlet 3.0 compatible versions. Or the other way is to make your application 2.5 compatible: add web.xml and define various components here. I'd prefer the first solution.