maven shade plugin custom transformer - java

Given maven shade plugin resource transformers, how can we create custom ones?
I've tried adding the shade plugin to my pom:
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.2</version>
</dependency>
And creating a class that implements ResourceTransformer. But when I run it, I get:
[ERROR] Failed to execute goal
org.apache.maven.plugins:maven-shade-plugin:2.4.1:shade (default) on
project foo: Unable to parse configuration of mojo
org.apache.maven.plugins:maven-shade-plugin:2.4.1:shade for parameter
transformers: Cannot load implementation hint
'test.transformer.TestTransformer' -> [Help 1]
The transformer is on the same classpath as the project I'm running the build on, which, I'm guessing is the problem. Is there a way to add in an extension that brings in other transformers?

See in the next section of the instructions:
create a maven project that contains test.transformer.TestTransformer with the appropriate dependencies in its pom.xml.
in the original project project's pom.xml in project.build.plugins.plugin[.id='maven-shade-plugin'] add the above maven project as a dependency.

Related

Convert maven to gradle?

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

Adding local jars to Java projects with Java 10

I have used Maven with earlier versions of Java but now I am trying to get it to work with Java 10. I built a simple Hello world project, specifically:
package main;
public class Test {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
With a module-info class:
module maven.test {
exports main;
}
The pom associated with this project is:
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
MavenTest
MavenTest
0.0.1-SNAPSHOT
MavenTest
src
maven-compiler-plugin
3.7.0
10
When I build the project, it build successfully. I then added a dependency from the Maven central repository:
org.jsoup
jsoup
1.7.3
This too builds successfully. I then add a locally installed jar.
<dependency>
<groupId>com.artificialmed</groupId>
<artifactId>DBconnectValueObjects</artifactId>
<version>1.0</version>
</dependency>
When I do this, I get a Build Failure with the following error message:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile (default-compile) on project MavenTest: Execution default-compile of goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile failed. IllegalArgumentException -> [Help 1]
[ERROR]
I've not found anything that would help me resolve this error. When I rebuilt the project using full debugging logging, I saw no useful information. I know the local jar was installed in my repository correctly because when I include that jar in projects compiled in Java 7, the project builds successfully. So, I suspect this is some issue related to Java 10 and locally installed jars.
Does anyone know how to get around this issue?
Elliott
I think this might be related to this question.
Basically, I don't think your problem is related to the local jar - If you've installed it in your local maven repository ($USER_HOME/.m2) using the maven install plugin, it has a pom.xml and to maven shouldn't be distinguishable from any other maven dependency.
The only thing I see "wrong" is the version of the maven compiler plugin - the current version is 3.8.0, which I'm using with Java 11 without any issues so far.
Also, note that the mechanism to set the source and target java releases changed, as in the following snippet:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>10</release>
</configuration>
</plugin>
</plugins>
</build>

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.

Maven error: Package matlabcontrol does not exist

I've added the following .jars as external jars to Libraries.
C:\Users\user\Downloads\common-lang3.jar\common-lang3.jar
C:\Users\user\Downloads\matlabcontrol\matlabcontrol-4.1.0.jar
However, during clean install of a Maven build in Eclipse, I get the following error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project com.webapp: Compilation failure: Compilation failure:
[ERROR] /C:/Users/user/workspace/GetTimeDistance/src/com/MatLabClass.java:[4,21] package matlabcontrol does not exist
[ERROR] /C:/Users/user/workspace/GetTimeDistance/src/com/MatLabClass.java:[7,40] package org.apache.commons.lang3.builder does not exist
The source of the jars in the library are pointing to the correct jars.
I am able to use the code in the jars in my own code.
When hoovering over the import, I get the following Note:
Note: This element neither has attached source nor attached Javadoc and hence no Javadoc could be found.
Any idea's? I already updated the Maven project several times.
EDIT:
I've added:
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.matlabcontrol</groupId>
<artifactId>matlabcontrol</artifactId>
<version>4.1.0</version>
</dependency>`
To the .pom file. This gives the following warning:
[WARNING] The POM for org.matlabcontrol:matlabcontrol:jar:4.1.0 is missing, no dependency information available.
When you run from maven, all the dependent jars will be referenced from maven repository only. It wont take any external libraries or jars. You define maven dependencies of the required jars in your pom.xml. If you want the local jars to be copied into your maven repository, below is the cmd mvn install:install-file -Dfile=your_local_path_of_jar -DgroupId=your_groupID -DartifactId=your_artifcat_id -Dversion=your_version -Dpackaging=jar
I assume you work with eclispe and added the external jars to the "Build path" of your Project properties? The "Build path" is used by the eclipse IDE to resolve classes within the workspace, and has no influence on which libraries are used when performing a maven build. Most likely, you missed to define commons-lang and matlabcontrol as dependencies in your pom.xml file...

Maven plugin loading parent project classses

I am creating a Maven plugin which I have included in my project. So my MOJO has my project reference but is there any way in which my MOJO can have all the dependencies of the project reference as well. The hierarchy is something like the following :
MyMOJO (plugin) is included as <plugin> in MyProject.
MyProject has a <dependency> of MyFramework I created.
Now I want my classloader to have the dependencies of MyProject as well as MyFramework.
Is there any way I can achieve this?
This worked for me :
We define mojo class with #Mojo annotation with the attribute requiresDependencyResolution = ResolutionScope.RUNTIME so that mojo resolves all the dependencies including transitives for the project.

Categories