Using Bamboo Automatic Dependencies with Gradle - gradle

We have a (very large) number of interdependent projects we're currently building monolithically with Ant and Jenkins, and we're in the process of switching to a modular build using Gradle and Bamboo.
The Gradle projects will use the Maven plugin to build JARs and POMs for deployment to a Nexus repository, but the 'official' dependency declarations are in the build.gradle files and the POMs are only transient artifacts.
Is there a way to get Bamboo's Maven-based automatic dependency management to work in this scenario? There are too many projects for it to be practical to configure and maintain the dependencies manually.

Related

can you build a maven project inside of a gradle wrapper

It may be just that I have a general misunderstanding how gradle build works, but it feels to me that I can not build a maven file inside of a gradle build. Since gradle uses the gradle.build file, and maven uses a pom.xml, it does not seem as though I can do this. I have multiple maven projects that I would like to wrap up with a gradle wrapper. I can not find ANYTHING on whether this is even possible.
Both Maven and Gradle are build tools and you should only use one of them for a given project.
If you have existing Maven projects and like the functionality provided by the Gradle wrapper, there is a similar wrapper for Maven (note that this is currently a third-party plugin but they plan to include it in the upcoming release 3.7 of Maven).
Alternatively you could convert your projects entirely to Gradle.

How to reference local Gradle project from Maven build as dependency?

I have several Gradle library projects and main Ant spring web-app project (historically). I'd like to replace Ant with Maven for main project while keeping existing Gradle projects nature.
Is it possible to refer local Gradle projects from pom.xml as local dependencies of Maven project?
Search readily gives me the opposite - "how to refer maven projects from gradle builds", but not my case.
Gradle always builds locally build/libs (or distributions); the only easy way to share the build dependencies between completely different projects is 'as maven repository dependencies'
in your case the options are
Work Local Builds only
add the maven plugin to the gradle builds - do local install
and refer them in the maven build locally.
Build Anywhere
Your Gradle builds publish artefacts to your local nexus
and you refer them properly in your dependencies
--
Gradle by default does not have 'maven install'; it can be added using the maven plugin see - https://docs.gradle.org/current/userguide/maven_plugin.html#header.

What is the difference between gradle install and gradle publishToMavenLocal?

In gradle, the install task is introduced by the maven plugin.
The publishToMavenLocal task is introduced by the maven-publish plugin.
The documentation of both tasks says they install/publish the module/artifacts to the local maven cache (.m2 directory).
If I need to publish a local project to my local maven cache, so that another local project can depend on it, which of the two tasks should I use?
As far as I know, these two accomplish the same thing.
That said, I recommend using the maven-publish plugin because:
It's newer and has a nicer publishing DSL, see the Maven Publish Plugin page for more details
It doesn't explicitly depend on the java plugin, which is useful if you ever build non-Java projects
You can always write: task install(dependsOn: 'publishToMavenLocal') if you like the task name install.

Building a p2 repository by resolving Tycho features from a Maven repository

I'm trying to build a p2 repository from Tycho feature artifacts which are deployed in a remote Maven repository, without having to install the artifacts into the local Maven repository first (as in Tycho fails to resolve reference from product to eclipse-feature from a different reactor build), and without having to build all features and the repository together in a single reactor build.
Background
I have a multi-module Tycho project that builds several Eclipse plugins and features.
So that I can build each module separately - and so that I can reference OSGI artifacts in our Nexus Maven repository - I have enabled <pomDependencies>consider</pomDependencies> in my target platform, and added Maven dependencies between the modules or to the repository artifacts as usual with <dependency/> elements.
This works well - I can build the features or run the plugin tests without their dependant plugins being either in my local Maven repository or in the same reactor build. For example, when I run mvn test on a plugin test project, the relevant dependencies will be downloaded from Nexus and Tycho will happily resolve the Import-Packages in my manifest against these, build everything and run the tests. So far so good.
I would like to generate a p2 repository from these features so that I can install them in Eclipse from an update site, and the advertised way to do this is with the eclipse-repository packaging type. But here the plan falls down - Tycho doesn't seem to be able to resolve feature dependencies when building repositories in the same way as it can resolve plugin dependencies when building features. All attempts yield:
[ERROR] Cannot resolve project dependencies:
[ERROR] Software being installed: my.eclipse.repository raw:0.0.1.'SNAPSHOT'/format(n[.n=0;[.n=0;[-S]]]):0.0.1-SNAPSHOT
[ERROR] Missing requirement: my.eclipse.repository raw:0.0.1.'SNAPSHOT'/format(n[.n=0;[.n=0;[-S]]]):0.0.1-SNAPSHOT requires 'my.prj.eclipse.project.feature.feature.group 0.0.0' but it could not be found
There are two ways I have successfully built the p2 repository:
As part of the same reactor build. If I make the eclipse-repository a module within the Tycho multi-module project, and build the whole project at once with e.g. mvn verify, the features are resolved fine. But I don't want to do this. I would prefer to build modules individually. This means our CI can have an indicator for each module, and we can immediately see what module tests have failed in; it gives us opportunities for parallelising builds; and we avoid having to be constantly running builds on modules that haven't changed. It would be a shame to have to use a monolithic Maven build.
If I install the Tycho project into my local Maven repository, by running mvn install on the dependency. But I don't want to do this either, because this would mean the build is inherently irreproducable, as it would be sensitive to the state of the local repository. Our CI is currently set up to maintain a Maven repository per job and to completely wipe it at the start of execution, to shield us from this potential messiness.
So my question is: is there a third way? Is there any way I can get the Tycho plugin responsible for building eclipse-repository packaging types to download features from a remote Maven repository? Or any other way I can build the p2 repository from plugins that have been individually built and deployed to the Maven repository?
Things I've tried include:
specifiying the Maven feature depedencies as both jar and eclipse-feature
explicitly adding the features to the target platform, like
...
<artifactId>target-platform-configuration</artifactId>
<version>${tycho.version}</version>
<configuration>
<dependency-resolution>
<extraRequirements>
<requirement>
<type>eclipse-feature</type>
<id>my.prj.eclipse.project.feature</id>
<versionRange>0.0.0</versionRange>
</requirement>
...
The closest thing I've found to a decent solution is have a multi-module Tycho project that just contains the repository and features.
feature-project
|- feature1 (eclipse-feature)
|- feature2 (eclipse-feature)
|- repository (eclipse-repository)
Building this works - all plugins added to the top-level POM are downloaded from Nexus, available for inclusion in each feature and included in the generated repository.
However this is far from ideal because I can no longer store my features logically alongside my plugins; they need to be in separate project hierarchies. Attempting to build the features and repository separately, like with mvn clean verify -pl :feature1,feature2,repository, fails presumably due to Bug 380152.
Is there a better way? Any help would be gratefully received.
Many thanks
(As an aside: building the repository with mvn clean verify -Dtycho.localArtifacts=ignore will succeed if the features are present in the local Maven repository, and won't show you the warning that artifacts are being resolved from the local repo... is this a bug?)
I am pretty impressed by your thorough analysis. You've almost got everything covered which is possible with the current Tycho version (0.22.0) - except for the solution which is so unintuitive that I wouldn't have expected anyone to be able to guess it (see below). Note however that there is a small fix required to also make the solution work for SNAPSHOT artifacts.
But first, I'd like to provide some technical (and historical) background for what you have observed:
pomDependencies=consider only works for plug-ins: The use case for this functionality was to allow referencing plug-ins (or more precisely OSGi bundles) from Maven repositories. So when the flag is set and the project has dependencies to JARs, Tycho will check if they are OSGi bundles, generate the p2 metadata for them on-the-fly, and add them to the target platform. There is no similar support for feature JARs because these usually don't exist in Maven repositories.
But what about Tycho-built projects? These may deploy into Maven repositories! Yes, this is true, and this is why I tried to extend the pomDependencies concept to allow for what you are trying to do. The idea was that every time Tycho considers a POM dependency for the target platform, it also checks if the p2 index files ...-p2metadata.xml and ...-p2artifacts.xml exist. However this turned out to infer a massive performance penalty because it generally takes very long for a Maven repository server to figure out that an artifact does not exist. So the remote download was disabled, and replaced with a look-up in the local Maven repository. In this way, two Tycho builds could set -Dtycho.localArtifacts=ignore and would still be able to exchange the artifacts specified in the POM via the local Maven repository.
Knowing these implementation details, we get to the following solution: Instead of only adding a POM dependency from the repository to the feature artifact, you also need to add dependencies to the p2metadata and p2artifacts files. Example:
<dependencies>
<dependency>
<groupId>myproject</groupId>
<artifactId>myproject.feature</artifactId>
<version>0.1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>myproject</groupId>
<artifactId>myproject.feature</artifactId>
<version>0.1.0-SNAPSHOT</version>
<classifier>p2metadata</classifier>
<type>xml</type>
</dependency>
<dependency>
<groupId>myproject</groupId>
<artifactId>myproject.feature</artifactId>
<version>0.1.0-SNAPSHOT</version>
<classifier>p2artifacts</classifier>
<type>xml</type>
</dependency>
</dependencies>
This makes Maven also download these p2 index files, so Tycho recognizes the main artifact as Tycho artifact. In this way, you can also get an eclipse-feature into the target platform via POM dependencies - at least almost: With 0.22.0, the repository build passes, but the feature.jar artifact is missing. I already debugged this issue, and it is easy to fix.
Obviously the syntax with three <dependency> elements for every actual dependency is not nice. It should be possible to boil this down to a single p2artifacts element - but this is more work. In case you are interested in this feature, you could open an enhancement request in Tycho's issue tracker.

How does Maven resolve sbt transitive dependencies? Is this Maven at all?

I have a maven project that depends on several sbt project, which in turn depends on more projects. Are these transitive dependencies being resolved by maven? E.g. in packing of assemby jar?
Let me answer this question with mine: How can you know what build tool was used in a dependency?
You can't unless the build tool leaves some files to let you guess what the build tool could have been. But still, they're just files that are generated as part of the publishing process so the other build tools could resolve dependencies.
That's the point of any build tool to let you integrate with the other build systems in a less troublesome way.
Maven requires pom.xml and an appropriate repository layout. sbt follows the rules while publishing project artifacts to Maven repositories.
Ivy requires other files in repositories and sbt does generate them (by default).
Gradle plugs itself in to the game by following Maven's standard files and directory layout.
Read about publish task. You may want to consult the official documentation of sbt. Start with http://www.scala-sbt.org/0.13/docs/Publishing.html.

Resources