Convert maven to gradle? - java

I have a project in maven. I want to convert project to gradle project how convert this code maven to code gradle:
<build>
<plugins>
<plugin>
<groupId>com.google.cloud.functions</groupId>
<artifactId>function-maven-plugin</artifactId>
<version>0.9.1</version>
<configuration>
<functionTarget>org.springframework.cloud.function.adapter.gcp.GcfJarLauncher</functionTarget>
<port>8080</port>
</configuration>
</plugin>
</plugins>
</build>

First install Gradle on your machine.
Now, go to your maven project’s root directory and execute command:
gradle init
Please note that gradle init automatically detects the pom.xml and creates a gradle build with the Java and maven plugin loaded. It means that existing Maven dependencies are automatically converted and added to your gradle build file.
So that the build.gradle file will be automatically created thanks to the gradle init command.And now your project is using gradle

I've never used function framework, however when it comes to plugin translation, these are pieces of code usually written by the maintainers of the framework, and usually they want to provide plugins for both maven and gradle.
Indeed in the documentation of the project, there is section for maven:
See Here
And also for gradle:
See Here
It looks like they offer to register the task in gradle, so its the best you can do with the existing state of the project, so its the way to go I believe

Related

Eclipse - Maven dependency is being ignored

I have a problem with adding maven dependencies to eclipse.
What should be OK:
pom.xml already contains all the dependencies and compilation and running tests using maven finished with success
all the source folders were recognized by eclipse
eclipse has Maven plugin and project is managed as maven project (see figure below)
pom.xml has maven-compiler-plugin, so the eclipse should know it is a maven project
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.1</version><!--$NO-MVN-MAN-VER$ -->
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
Issue:
eclipse does not resolve dependencies defined in pom.xml (guava, juint and other libraries are in pom.xml but not in eclipse class path)
any right click > Maven > Update project has no any effect (see figure below)
I noticed that libraries does not contain any Maven Managed Dependencies - by adding as described below has no any effect
My attempt to add Maven Managed Dependencies to Eclipse manually
1) Add library
1)
This attempt has no any effect and "Java Library Path" remains the same even after this action.
2)
Command
mvn eclipse:eclipse
Fails with
[ERROR] Failed to execute goal on project pmml-model: Could not resolve dependencies for project org.jpmml:pmml-model:jar:1.3-SNAPSHOT: The following artifacts could not be resolved: org.jpmml:pmml-agent:jar:1.3-SNAPSHOT, org.jpmml:pmml-schema:jar:1.3-SNAPSHOT: Could not find artifact org.jpmml:pmml-agent:jar:1.3-SNAPSHOT in sonatype-nexus-snapshots (https://oss.sonatype.org/content/repositories/snapshots) -> [Help 1]
The jar does not exist in the desired version on the server your maven script is referencing. The most recent version is 1.2.9. The snapshot version is not present. You should reference
<dependency>
<groupId>org.jpmml</groupId>
<artifactId>pmml-model</artifactId>
<version>1.2.9</version>
</dependency>
in your pom.xml.
The below jars are not present in your remote repo.
org.jpmml:pmml-agent:jar:1.3-SNAPSHOT,
org.jpmml:pmml-schema:jar:1.3-SNAPSHOT
Open the url https://oss.sonatype.org/content/repositories/snapshots in your browser to see the available snapshots/
Apparently, you are trying to set up a project that makes use of the JPMML-Evaluator library. In that case, you should only depend on the latest stable org.jpmml:pmml-evaluator dependency (which is version 1.2.13 at the moment). In other words, don't try to manage the associated org.jpmml:pmml-model, org.jpmml:pmml-schema dependencies manually.
You could base your work on the JPMML-Evaluator-Bootstrap project instead.

How do I create an Eclipse plugin release using Maven and Tycho?

I am building an Eclipse project that consists of a number of plug-ins that are packed together. I have create POM files for each component and a main POM for the project. Something like this:
projectDir\releng\pom.xml <-- Parent project
projectDir\proj1\pom.xml <-- Child project 1
projectDir\proj2\pom.xml <-- Child project 2
My build currently works by calling the parent POM which builds everything. Until now I have been building using 0.0.1-SNAPSHOT as the version of the parent POM, and in each Eclipse plug-in I have 0.0.1.qualifier as the version in the MANIFEST.MF file.
I now want to promote my latest version to 0.1.0. From my understanding, this means that I have to go over ALL of my POM files AND MANIFEST.MF files and upgrade the version in both of them (since while the version is defined in the parent POM, it is referenced in all child POM:s).
Is this the correct way to do this or is there a way to automate the whole process and not make mistakes?
P.S. There is the Maven Release plugin but this won't work with Eclipse.
For the version update step of a relase process, there is the tycho-versions-plugin which knows how to consistently update the POMs and manifests.
Just go to the root of your parent/aggregator module and call
mvn org.eclipse.tycho:tycho-versions-plugin:set-version -DnewVersion="0.1.0"
This will update the version of the parent project and of all child projects with the same/equivalent version as the parent project. In your case, these are all projects because the Eclipse versions 0.0.1.qualifier is considerered equivalent to 0.0.1-SNAPSHOT in Tycho.
For the remaining steps of the release process (tagging, building, pushing tags, etc.) just call the appropriate SCM or Maven commands, e.g. from a script. I haven't tried to use the maven-release-plugin for this (and apparently no-one else has).
Please have a look here: Unleash Maven Plugin - Tycho Releases
The Unleash Maven Plugin is implemented as an alternative to the Maven Release Plugin and has a Tycho feature which should do exactly what you need. Furhtermore it is much more flexible, failure tolerant and has an integrated rollback feature.
I will publish some blog posts soon to promote and explain this plugin.
just some hints on how we implemented it.
It can be done with an extra plugin that does transformation of versions in MANIFEST.MF and *.product files. This plugin needs to be a lifecycle participant. #Component(role = AbstractMavenLifecycleParticipant.class) the reason for this is that is must transform and commit before the release plugin starts to look for modifications. Then it must also to transformation back after the release.
The mojo executor plugin saves a good deal of work since it can call the replacer, buildhelper and scm plugin from inside your plugin.
Another important gotcha is that you need to disable to hard coded clean invocation that tycho does by confguring the release plugin to configure the clean plugin to skip execution.
Hope this helps.
There is a new feature in tycho-1.1.0 (unreleased at the time of this post) that should support what you're trying to do.
If you've configured your pom correctly for standard maven-release + added the dep to tycho 1.1.0, you can customize your build as follows [1]:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
<configuration>
<preparationGoals>org.eclipse.tycho:tycho-versions-plugin:${tycho-version}:update-eclipse-metadata org.apache.maven.plugins:maven-scm-plugin:1.9.5:add org.apache.maven.plugins:maven-scm-plugin:1.9.5:checkin</preparationGoals>
<completionGoals>org.eclipse.tycho:tycho-versions-plugin:${tycho-version}:update-eclipse-metadata org.apache.maven.plugins:maven-scm-plugin:1.9.5:add org.apache.maven.plugins:maven-scm-plugin:1.9.5:checkin</completionGoals>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-scm-plugin</artifactId>
<executions>
<execution>
<id>default-cli</id>
<goals>
<goal>add</goal>
<goal>checkin</goal>
</goals>
<configuration>
<includes>**/META-INF/MANIFEST.MF, **/feature.xml, **/*.product</includes>
<excludes>**/target/**</excludes>
<message>Changing the Eclipse files versions</message>
</configuration>
</execution>
</executions>
</plugin>
[1] This is taken directly from a tutorial that describes this new feature:
https://wiki.eclipse.org/Tycho/Release_Workflow

How to use custom plugin in maven pom.xml?

I created one custom plugin in maven. So when run this project then automatically created related one JAR file. So now i created one more project in maven in this project how to use that jar file in the pom.xml file. Is there any possible to use already existing jars in pom.xml file.
I'm a little unsure of what you're getting at- my understanding is that you wrote a plugin that you now want to use in a separate project. In order to do this, you would add the custom plugin to your section in the new project. It will resolve this the same way it does all plugins. You also need to deploy the plugin to whatever pluginRepository you're using.
For example, I have a plugin, which is defined in its pom as:
<groupId>org.mine</groupId>
<artifactId>my-maven-plugin</artifactId>
<version>1.0</version>
<packaging>maven-plugin>
which is what it seems you already have.
Then, run mvn deploy.
In the project you want to use the plugin is, add to your pom:
<build>
...
<plugins>
...
<plugin>
<groupId>org.mine</groupId>
<artifactId>my-maven-plugin</artifactId>
<version>1.0</version>
</plugin>
...
</plugins>
...
</build>
Finally, make sure you have the repository you deployed to set up in your pom.xml in the tag.

How does Maven understand release:prepare command?

I'm trying to create a plugin which would download and install jars from Maven central as system tools. So I want my line to be like
mvn install-plugin:install org.chaschev:cap4j:1.0
similar to Ruby's
gem install capistrano
This plugin would gather all the needed information about the shortcuts to create from the JAR. I.e. this jar would contain a class implementing an installation interface.
How does Maven understand that in order to execute a command like release:prepare it requires to download the release plugin and to run it? Any better/other way to do this?
Do you mean how the relation between plugin/goal in the comamnd line and plugin implementation is defined? Then the answer is plugin.xml. See plugin.xml for release plugin, e.g. maven-release-plugin-2.0.jar:
<goalPrefix>release</goalPrefix>
...
<mojos>
<mojo>
<goal>help</goal>
...
<mojo>
<goal>prepare</goal>
...
Or do you mean, how Maven discovers which plugins are available? Then the answer is:
There are two default groups where plugins are searched, org.apache.maven.plugins and org.codehaus.mojo
For your own plugin you may want to use name ${prefix}-maven-plugin, e.g. cap4j-maven-plugin
You can keep your name cap4j, but then put the plugin description to your POM, under <plugins>
If you want your build to work at other machines, they should point <pluginRepositories> in POM or in settings.xml to your plugin repository
It is not good to use default Maven groups for your own project.
Instead, define your own group for your plugin, like this:
<pluginGroups>
<pluginGroup>org.chaschev</pluginGroup>
</pluginGroups>
And rename your plugin from cap4j to cap4j-maven-plugin. Then Maven will discover your plugin without further cahnges in POM.
Alternative, without <pluginGroups>, just put following to your POM:
<plugins>
<plugin>
<groupId>org.chaschev</groupId>
<artifactId>cap4j</artifactId>
<version>...</version>
<configuration>
...
</configuration>
</plugin>
...
</plugins>

maven surefire reporting plugin configuration

I have a multi-module maven project. The parent pom.xml is simply a way to reference common information for the 4 subprojects. I have quite a few JUnit tests that run and I also have the Parent Project set up for Project WebSite using the maven-info-reports-plugin.
I have the maven-surefire-report-plugin configured in the parent and it generates the target/site/surefire-report.html file in each of the subprojects with the correct information.
My problem is when I run my project website via site:run I do not see any of the surefire-report.html files in the Project website. The one that shows is in the target directory of the parent and it has no unit tests defined.
Is there a way I can configure maven-surefire-report-plugin or maven-info-reports-plugin to aggregate the subprojects generated surefire reports?
To elaborate on Seph's answer. You can set many of the Maven reports to aggregate results. To do this with the surefire-report plugin you'd do something like this:
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<aggregate>true</aggregate>
<!--also set this to link to generated source reports-->
<linkXRef>true</linkXRef>
</configuration>
</plugin>
</plugins>
</reporting>
Note the additional linkXRef property, this allows you to add cross-references to the generated html version of the source produced by the jxr plugin. The jxr plugin can also be set to aggregate, so the two combined allow you to browse your entire project structure.
As far as I know, the maven-info-reports-plugin doesn't do aggregation.
You can add
<aggregate>true</aggregate>
to the surefire plugin in the parent pom.xml.
For command line
mvn surefire-report:report -Daggregate=true
It could be -
mvn clean test -fn surefire-report:report -Daggregate=true
OR
mvn clean install -fn surefire-report:report -Daggregate=true
Note :
fn -> NEVER fail the build, regardless of project result
To add in pom
<aggregate>true</aggregate>

Categories