Creating a project builder and plugin in Gradle - plugins

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.

Related

How do I make a multiproject gradle build in STS eclipse?

I have 2 separate projects in STS Eclipse, both using gradle to build. One is a web application. The other is a data access library that the web application uses. I have the buildship plugin for this.
Normally, I would simply change the project properties to include the data project in the web application's build path/project references/etc to make this work, but we're switching over to using Gradle for this.
I have a working build.gradle file in each project, but I don't know how to make the web app's build script build and include the data project.
I've looked for various tutorials and examples, and they talk about a root project that contains both projects inside. I have no idea how to create this.
How do I go about setting this up?
Furthermore, I'm concerned about SVN in a setup like this. I want to be able to commit each project separately since other applications will use the data access library, which is why it's a separate project. My understanding is that a nested project structure suggested by gradle would cause the entire root project to be committed as 1 entry, with both subprojects.
This would then mean that I would have to copy the data access project into all the other projects that need to use it, which would make maintaining the code a pain. At that point, I might as well not have a separate data access project and instead just include all that code in a package in the web app.
The solution to this issue was the "includeflat" command in the settings.gradle

Working modular example for JavaFx8 + OSGi + Gradle multiproject without additional tooling?

I am stuck with a problem I can't solve for weaks now.
I have to create a modular JavaFX application, where each component defines a "domain unit" (with models+views+controllers). Each component can be loaded into a "shell application" (as a content of a tab view or multiple tab views) and the modules can depend on another module(s) (their content in tabs won't appear if their dependency is not loaded).
That's why I was planning to create an OSGi based JavaFX application and build it with Gradle as a multiproject.
I've already tried dozens of tutorials with no success and I experienced, that most of these tutorials:
are outdated, not reproducible (e.g. elcipse's interface, templates have changed, bndtools tutorials doesn't seem to work, equinox doesn't seem to to work without felix, javafx8+osgi generate different kind of problems... etc.)
are too complex for a beginner (I just started to learn OSGi and Gradle) and they skip important steps I am not aware of
contain too much "IDE magic" (I would rather type some code instead of filling forms in eclipse)
some solve the problem with different tools (maven/tycho, bndtools, e(fx)clipse), but I've got no time to learn them
I want my application to be independent from IDE's environment. I don't want to use e(fx)clipse or BndTools if possible (even if they can make the build process easier)..
I'm experimenting with OSGi implementations, that's why I would rather not to choose between Equinox, Felix or Karaf.
I've already programmed similar application in .NET world, but it seems to me impossible to do the same in Java world..
My main questions are:
is it possible to do what I have imagined?
how to create a gradle multiproject what is IDE/platform independent (if projects are not tied to eclipse environment, or equinox, but it's possible to use them)?
what are the best ways to initialize the application (shell application + modules) and load the independent modules/bundles/components?
how to separate my views into subprojects (what build.gradle files should contain)?
how to solve the javafx8 inpompatibility with osgi?
what is the correct way to apply javafx plugin in gradle?
what tutorials are the most relevant?
is there any working example, pattern or tutorial (without using additional tools) what solve the same problem (using only osgi+javafx+gradle)?
I could group your questions in differents topic:
OSGI
You just need to google around to find out that is a java specification that encourages modularization, provides hot-deploy feature, and so on. As I told you, is just an specification like Java Servlet API, so they are different providers or implementers of OSGI Specification such as Felix and Equinox. Karaf instead is a OSGI container based on Felix, so you get all felix benefits and in addiction karaf natives features. For that reason I encourage you to take Karaf into use.
Aquote BndTool
In order to satisfy the OSGI specification, you need that your modules contains a MANIFEST.MF which holds all dependency information so Karaf create the classloader required for your bundles.
Assuming that you don't want to create that MANIFEST.MF files by hand, you could take aqute/bndtool for that. Don't get mess with bndtool for eclipse plugin. That application can be used from command line, from a maven plugin, or from a gradle plugin. Basically scans your classes, check the imports, and create a MANIFEST.MF automatically.
Gradle
If you choose gradle as a build tool, then you can take into use: Bnd Gradle plugin. It's easy to set up, but follow the instruction for non-workspace plugin. If you don't want to use BndTool for eclipse. IDE independent solution, you mentioned in your question.
MultiProject Layout
How the project layout should look like, depends on your modularization, but you can have a look on this layout example that uses gradle+osgi+karaf for a multiproject. Perhaps inspires you.
https://github.com/antoniomaria/gradle-karaf-bnd-project

Global Plugins in Gradle

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.

Managing custom client builds with SBT

We have an application that is extensible via modules. The (multi-project) SBT build produces a distribution artifact that is easy to deploy.
Some custom deployments for clients do require specific modules to be part of the build (in other words, an additional set of dependencies). I'm wondering what would be the best approach to create such custom builds - in other words, is there perhaps a way to extend the main Build and only add those dependencies?
Right now I am thinking of the following approach:
have the main application packaged (as ZIP) & released
in the custom build, fetch the ZIP file, extract it, magically add the additional JAR dependencies, and zip the artifact again ("magically" because I don't know how to get access to all JAR dependencies specified in the build)
But is there perhaps a more elegant way?
I think it would be easier to just declare a new sub-project along the main one that depends on it.
lazy val extra: Project = Project("extra", file("extra")) dependsOn(mainProject) settings(Seq(...))
Then on that package you can declare the extra dependencies. When you package this extra project everything should end up into the package automatically.

Custom project structure in eclipse

I often use a eclipse plugins to quckliy generate project directory structure, for example Spring plugin helps to create WebContent folder to hold WEB-INF contents. But sometimes I create a projects with specific directory structure, always the same. Something like this:
[project_name]
-folder1
-folderA
-folderX
-folderB
-folder2
-dirC
-folder3
I know, that I can write my own ant/maven script to generate specific folders, but I don't want to manualy invoke scripts each time after project creating. I want to create a project template which I would use in the future.
What is the best way to create a this feature?
Should I create something like plugin? If yes, how would I do it?
If you don't want to use a plugin or a script to generate the project structure, you're pretty limited.
Since you can create a project from existing source, you could create the folder structure on your filesystem, and whenever you are creating another project, tell Eclipse to "Create Project from existing source" and point it to your template folder.
If you want to write a plugin, you'll want to contribute a org.eclipse.jdt.ui.actions.OpenProjectWizard that creates the folder structure as part of the project generation. A good tutorial can be found at: http://cvalcarcel.wordpress.com/2009/07/08/writing-an-eclipse-plug-in-part-1-what-im-going-to-do/%20
Maven has a concept of project templates called archetypes which is very similar to what you need. It defines the project structure, files, etc and can be customized upon creation.
If you install the Eclipse m2e maven plugin then you can create a new project from template with a few click
File / new Project / Maven project / Select archetype
You can use this feature even if you dont want to use maven later. You can auto generate ant scripts or anything like that. You can convert an exisint project into an archetype or create a new one from scratch, desciption is here
http://maven.apache.org/archetype/maven-archetype-plugin/
http://maven.apache.org/guides/mini/guide-creating-archetypes.html