Global Plugins in Gradle - plugins

I am using Gradle as a build tool for my Scala projects. I am also using Eclipse for development, so I always use the apply plugin: 'eclipse' in my build files.
Is there also a way to define the apply plugin globally? SBT already has such a feature. The reason why I want this, is that other developers who’re using my project probably don’t use Eclipse but another IDE and they would have to change the build script for their needs. If there were a global configuration file, one could put personal configurations in there and it wouldn’t conflict other ones.

You can put this into ~/.gradle/init.gradle:
rootProject.allprojects {
apply plugin: "eclipse"
}
The drawback of this approach is that it makes the build less reproducible. People now need to add something to their init.gradle to make (some aspect of) the build work. Therefore, I would recommend to apply the Eclipse plugin in the main build script; it won't hurt people who don't use it.

for the accepted answer, I also got the error message "The root project is not yet available for build".
I am using gradle 7.6 and I was trying to use the Gradle Test Logger Plugin
Here's the ~/.gradle/init.gradle that worked for me:
initscript {
repositories {
gradlePluginPortal()
}
dependencies {
classpath 'com.adarshr:gradle-test-logger-plugin:3.2.0'
}
}
allprojects {
apply plugin: com.adarshr.gradle.testlogger.TestLoggerPlugin
}
There are multiple things to be aware of when configuring a 3rd party plugin globally:
you need to add the plugin as a dependency to make it available
you need to reference the plugin by its implementation class rather than its ID
you must not use quotes around the implementation class
The last two points are described in the Gradle Forums question
Cannot apply plugin by id in init.gradle script.
How to Find the Implementation Class
If you wonder what the implementation class is for your plugin, here's how I found it for the Gradle Test Logger Plugin:
Open the project on github
Look at it's build.gradle file
Look for the section gradlePlugin {
Take the value from implementationClass
for example: https://github.com/radarsh/gradle-test-logger-plugin/blob/29af9282761f003f51d49aa804cc72e35b16c33b/build.gradle#L61
Remember to not copy the quotes :)

You could create a build.gradle in buildSrc which implements your custom logic. Just don't check it into version control. To apply it to multiple projects you could create a central buildSrc directory and create symbolic links in the projects that you want to apply it to.

Related

gradle: can you apply plugin from command line

I downloaded an open source gradle-built project and ran gradle eclipse but apply plugin: 'eclipse' was not in build.gradle so it failed. I edited the file and added the line then successfully ran gradle eclipse. I found this a bit clunky inasmuch as I'd prefer to be able to run gradle eclipse with some flag for instance so that I don't need to change the source at all. Is there an alternative to my action?
My approach to this problem is to write custom gradle scripts in ~/.gradle/init.d/ such as this one:
allprojects {
project.apply plugin: 'maven'
buildscript {
repositories {
mavenLocal()
}
}
repositories {
mavenLocal()
}
}
(this one is to ensure I can leverage mavenLocal repository in every gradle project, but you can do the same and apply eclipse plugin)
This scripts are executed in every gradle build (in alphabetical order, so using a convention similar to Linux init.d in naming can help, e.g. 00-eclipse.gradle) and can enrich your build.
I favor this approach for things which would be user-specific and don't deserve to be in everyone's build script (eclipse/idea/mavenLocal fit that category, and I also apply gradle-tools-plugin for easier debugging through command line.
Note that like Ben mentioned, I usually recommend against using the eclipse/idea project generation plugins, but prefer the IDE gradle integration, but that's another story.

Get Jenkins plugin dependencies auto installed

I'm developing Jenkins' plugin which is dependent on another plugin (specifically MultiJob plugin, but it can be any other one of course).
Obviously, the dependency is found in POM, so I can actually use the classes of it.
The problem: if I'm trying to install my plugin in Jenkins that the dependency is not found in it Jenkins doesn't installs it automatically and upon first usage my plugin throws an exception NoClassDefFoundError, of course.
Question: can I make Jenkins to install my dependencies as prerequisites and if yes, how?
Note: I do see that other plugins somehow cause the dependencies to be installed (Git plugin for instance makes GitClient installed during its installation).
Thanks in advance.
It's been a while since i've raised the question, but if anybody will look for something similar, here is what i've finally came up with:
Since the dependency classes are only needed in case they are really there, i've decided to use Java's lazy linkage behavior and actually refer the relevant classes only on demand.
So practically, made a factory that has a list of class names of interest and every time i need to process some object i'm checking it's class against this list. If matched - the class is loaded and therefore it's okay to init the linking/initiation logic.
Last one, if you plan to use such a pattern do not forget to sign those dependency plugins as optional in your pom.xml.

How to build RCP application based on Product Configuration and Target Platform Definition?

I'm about the setup an automatic (command-line) build for my Eclipse RCP Application.
I have found out the following ways to do it:
Buckminster
Using Maven with the pde-maven-plugin
Headless PDE Build
The problem with all these options is that they require me to create essentially a new representation of the information already contained in my target platform definition. For example in Buckminster, this would be the .rmap file.
In my thinking all the information to build the product should be already there when I have the following:
Plugin project with product configuration file (foo.product)
Target platform definition file (foo.target)
Therefore I would expect there to be a command like the following:
build-rcp-product foo.product foo.target win32
Is there anything like that which I may have missed?
With Buckminster you don't need to replicate the information in your target definition file. You can simply import the target file using the importtargetdefinition command. If all your dependencies defined in the target definition file, then in the rmap you define only from where to materialize your plugins (svn, git, maven, file system etc).
With PDE build, there is a filed request (Bug 266311) and it seems it is still not possible to utilize the target file directly but there are some workarounds suggested in there (which I didn't try, I am using Buckminster).
I use the PDE build and it's pretty simple. It essentially gets what it needs from the MANIFEST.MF file and the build.properties file.
The command to run it is more complicated, as you have to start Eclipse and point it to a few things, but it's very well integrated with the IDE. It does everything by making Ant scripts.
you can try tycho
here's a good start:
Tycho tutorial
Reference card
with tycho, all you need is a POM and you usually will not change this information, which is generated via maven

Creating Single file Runnable Jars in Scala with Eclipse

Can anyone tell me how I can create a runnable JAR file from a Scala program using Eclipse and Scala 2.9.x ?
A quick Google brings up results like this ..
http://garyboone.com/2009/06/creating-single-file-runnable-jars-in-scala-and-eclipse/
Which uses a Java class to call the Scala main however it doesn't seem to work with Scala 2.9.
Thanks for your time.
Ian
In Scala 2.9.1, you can simply create a .java file like this to be able to make use of Eclipse's Create Runnable JAR functionality:
public class ScalaRunner {
public static void main(String[] args) {
Main.main(args);
}
}
If, by chance, you're using sbt to build then you can use the assembly-sbt plugin. We use this at Yammer to build fat JARs of our infrastructure services. We're (gradually) switching over to use maven and the shade plugin. I believe shade does a slightly more intelligent job of merging JARs than assembly-sbt does. However, we've had great success so far with assembly-sbt.
Not Eclipse specific, but I would use sbt with the Proguard plugin: https://github.com/siasia/xsbt-proguard-plugin
This lets you setup a reusable build without depending on Eclipse or similar IDEs.
Proguard is a great tool to create an executable jar -- if you're not using sbt (if so, use the sbt-proguard-plugin other folks are advising) a standard way to build the executable jar from within eclipse would be to build an ant build file, and then reference that ant build from within eclipse.
Almost all android developers who use scala use proguard, and you'll find lots of documentation from them about this.
Here's proguard documentation about creating an ant task:
http://proguard.sourceforge.net/index.html#/manual/ant.html
And step 4 from this page about android development from scala IDE wiki has the specific steps for adding the ant build file to eclipse.
http://www.assembla.com/wiki/show/scala-ide/Developing_for_Android

Creating a project builder and plugin in Gradle

I want to use Gradle to create following things.
I want to create a plug-in which can
Create a project structure
project life cycle of my one like plugin 'java' has many java related lifecycle like i need my own lifecycle
Internally jars or zips my project
runs my project.
How to implement all these.. any example ?
most of what you are asking for is information provided in the cookbook: http://docs.codehaus.org/display/GRADLE/Cookbook
to create a project structure read: mrhaki.blogspot.com/2009/11/using-gradle-for-mixed-java-and-groovy.html
and look for the initProject task.