How does exec.mainClass work without exec-maven-plugin? - java

I have a project that uses the MojoHaus Exec Maven plugin to run some Java code. Here's the pom.xml file:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.happycoding</groupId>
<artifactId>google-cloud-vision-hello-world-standalone</artifactId>
<version>1</version>
<properties>
<mainClass>io.happycoding.vision.CloudVisionHelloWorld</mainClass>
<exec.cleanupDaemonThreads>false</exec.cleanupDaemonThreads>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-vision</artifactId>
<version>1.100.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>${mainClass}</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
This works fine, and I can run my code using this command:
mvn clean package exec:java
I understand that the exec-maven-plugin plugin, which is specified in the plugins tag, runs the code using the mainClass property.
I was surprised to find that this pom.xml also works:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.happycoding</groupId>
<artifactId>google-cloud-vision-hello-world-standalone</artifactId>
<version>1</version>
<properties>
<exec.mainClass>io.happycoding.vision.CloudVisionHelloWorld</exec.mainClass>
<exec.cleanupDaemonThreads>false</exec.cleanupDaemonThreads>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-vision</artifactId>
<version>1.100.0</version>
</dependency>
</dependencies>
</project>
This file specifies the exec.mainClass property, but does not specify any plugins. However, I can still run my code just fine with this same command:
mvn clean package exec:java
But I don't understand how Maven knows to run this command without any plugins specified.
Is the Exec Maven plugin somehow automatically installed in Maven by default? Or is exec.mainClass somehow setting a property used by a different default tool within Maven?
I tried reading the official documentation, but I didn't see anything that mentions whether the plugin is included by default.
I've found that I can also pass the exec.mainClass property in as a command line argument, but I still don't understand how Maven knows what to do with that without the plugin being explicitly defined.
I much prefer the shorter file, but I want to make sure I understand how it's working and that I'm not missing anything that's going to bite me later.

When you specify exec:java, you are specifying a plugin, specifically exec-maven-plugin (along with the goal java). Other common plugins that are used by being explicitly identified on the command line rather than being attached to phases such as clean or package include versions, dependency, and archetype (this last of which doesn't even require a POM to be present, since it generally creates new ones).
Note that in your POM you don't attach exec to any phases (that plugin usually isn't); therefore your plugin entry serves only to provide configuration settings in the case you run the plugin explicitly from the command line, in your specific case equivalent to the exec.mainClass property.

Related

Ascii-Doc Maven Plugin (include resources via https)

Is there a way to configure the asciidoc-maven-plugin to include https-resources? I did some research and found the follwing (interesting) information, however, was not able to solve the problem according to the hints provided:
https://github.com/asciidoctor/asciidoctor/issues/1049
https://github.com/asciidoctor/asciidoctor-maven-plugin/issues/153
The latter link seemed to adress the exact problem I have, however, upgrading / changing versions did not have any effect.
The following pom.xml file is a minimum working example that allows you to replicate the problem:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.myproject</groupId>
<artifactId>my-project</artifactId>
<version>${revision}</version>
<packaging>pom</packaging>
<properties>
<revision>1.0.0-SNAPSHOT</revision>
<asciidoctor.maven.plugin.version>2.2.1</asciidoctor.maven.plugin.version>
<asciidoctorj.diagram.version>2.2.0</asciidoctorj.diagram.version>
<asciidoctorj.version>2.5.2</asciidoctorj.version>
<jruby.version>9.2.17.0</jruby.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctor-maven-plugin</artifactId>
<version>${asciidoctor.maven.plugin.version}</version>
<executions>
<execution>
<id>output-html5</id>
<phase>generate-resources</phase>
<goals>
<goal>process-asciidoc</goal>
</goals>
<configuration>
<sourceDirectory>src/main/docs</sourceDirectory>
<outputDirectory>target/docs</outputDirectory>
<backend>html5</backend>
<doctype>book</doctype>
<attributes>
<safe-mode>UNSAFE</safe-mode>
<allow-uri-read>true</allow-uri-read>
</attributes>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctorj</artifactId>
<version>${asciidoctorj.version}</version>
</dependency>
<dependency>
<groupId>org.jruby</groupId>
<artifactId>jruby-complete</artifactId>
<version>${jruby.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
Now, if I save the following .adoc-file to the src/main/docs folder and run mvn clean install, I just get a notice Unresolved directive in my-project.adoc - include https://raw.githubusercontent.com/asciidoctor/asciidoctor/master/README.adoc instead of the actual document:
= my-project
include::https://raw.githubusercontent.com/asciidoctor/asciidoctor/master/README.adoc[]
However, if I donwload the above document (README.adoc) and reference it locally, I get the desired result. But this is not the way I want to include documentation by first downloading the file and then referencing it locally.
Does someone have an idea?
The idea is to only manage a single Maven project that contains all documentation including documentation from related projects that are also in our Gitlab repository. That is why I wanted to reference .adoc-files in those related projects by using include::https://concreteURLToAdocFile.
However, we realized, that it is a better idea to use the maven-dependency-plugin to first copy the .adoc-files from the other projects and then reference them locally.
This solution solved our concrete problem, however, it did not answer my original question.

How to deal with dependencies of generated sources in Maven?

For the last few days, I have been trying to get the code for a REST-API client being generated into my project using the openapi-generator-maven-plugin. However, Maven fails in the compile-Phase as soon as it realizes that the third-party dependencies just introduced by the generated code have not been resolved yet. As the code generation itself works, my problem seems to be rather general and related to what's happening after code is being generated.
This is a sample POM, containing the copy-pasted contents of their own README on how to set the plugin up:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>openapi-generator-maven-plugin-error-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<!-- RELEASE_VERSION -->
<version>5.0.0</version>
<!-- /RELEASE_VERSION -->
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v2.0/json/petstore.json</inputSpec>
<generatorName>java</generatorName>
<configOptions>
<sourceFolder>src/gen/java/main</sourceFolder>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Running mvn clean compile should generate the sources, and it does. However, as soon as the maven-compiler-plugin kicks in right after the code is being generated, it sees newly introduced package and symbol references in the generated code and consequently fails.
So I have tried to figure out how to install said dependencies, AFTER generation, but BEFORE compilation, but completely failed to do so. I suspect I need to use the maven-depndencies-plugin, but due to my limited knowledge of Maven and even more limited knowledge of how its plugins work, I am completely lost. I have tried adding the maven-dependency-plugin to my POM, but Maven completely ignores it and does not run it at all. I have tried to bind the resolve goal to the compile phase to no avail.
I would have expected code generation to not be a too uncommon thing to do with Maven, so I am a bit surprised it is actually so hard to pull off. Am I on the right tracks here? What exactly am I missing to have those transitive dependencies introduced in the generated code installed before the compiler is trying to compile them?

Maven (eclipse) use shaded jar instead of original jar

I have a project, where the packages will be relocated (shaded jar)
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>artifact-child</artifactId>
<name>artifact-child</name>
<parent>
<groupId>group</groupId>
<artifactId>artifact</artifactId>
<version>${revision}</version>
</parent>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<!-- <shadedArtifactAttached>true</shadedArtifactAttached> -->
<relocations>
<reloaction>
<pattern>example</pattern>
<shadedPattern>${super-package}.example</shadedPattern>
</reloaction>
</relocations>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
And I have a second project, which need the shaded jar at runtime.
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>testversion</groupId>
<artifactId>testv</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>testv</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>group</groupId>
<artifactId>artifact-child</artifactId>
<version>0.0.1</version>
<classifier>shaded</classifier>
</dependency>
</dependencies>
</project>
My question:
Eclipse find the other project in his workspace and use directly the source code.
For example: I must write import example.myclass instead of import org.example.myclass.
In some cases, this can be a problem. Is it possible to say, what maven or eclipse should use the "shaded jar" instead of the original source code?
Must I create a online repository, so maven can only download the shaded jar?
I found two other stackoverflow posts (with no result):
Maven Eclipse multi-module shaded dependency
How to install shaded jar instead of the original jar
SOlVED:
My Mistakes:
1. version of the parent must declare directly not via properties
2. Forgot to run "Maven Install"
Solution:
Maven run without errors, but Eclipse use the open project and not the shaded jar.
Found the solution here: How can I download a single raw file from a private github repo using the command line? .
Open the properties of the Project. Under the Tap Maven, remove the check from "Resolve dependencies from Workspace projects"
If the shaded jar is your dependendy, you just reference the classes/packages as they are defined in the shaded jar.
If Eclipse shows an error, Eclipse is wrong (which is often the case). First of all, try to build your project with something like clean verify. Then you see whether you really have errors (or if Eclipse made them up).
If Eclipse errors bother you, try ALT+F5.

spring-boot Maven: How to create executable jar with main class?

Recently, I wrote a Spring-Boot project and I wanted that Maven will be able to create a jar file that I'll be able to run by the command "java -jar ".
This is the pom.xml I wrote:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SpringBootGame</groupId>
<artifactId>SpringBootGame</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.3.RELEASE</version>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.3.RELEASE</version>
<configuration>
<mainClass>com.game.Main</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
In order to build the jar file, I had to run the command: "mvn clean package spring-boot:repackage".
My questions are:
Why must I add spring-boot-maven-plugin? I already have spring-boot-starter-web dependency that adds spring-boot as a dependency and maven-compiler-plugin that builds a jar file from the code?
Can you think about a way to configure pom.xml file that I'll be able to get the jar file using the "native" command "mvn clean package" and not the cumbersome "mvn clean package spring-boot:repackage"?
Thanks
Why must I add spring-boot-maven-plugin? I already have spring-boot-starter-web dependency that adds spring-boot as a dependency and maven-compiler-plugin that builds a jar file from the code?
Because the plugin is what adds Maven support for Spring Boot
Can you think about a way to configure pom.xml file that I'll be able to get the jar file using the "native" command "mvn clean package" and not the cumbersome "mvn clean package spring-boot:repackage"?
It looks like you are missing the <packaging>jar</packaging> element in between your <project> </project> element.
The reason you had to run that lengthy command, is because you did not include the <executions></executions> element when including the plugin. Please see section 71.1 of the following docs:
https://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-maven-plugin.html#build-tool-plugins-include-maven-plugin
71.2 elaborates on the <packaging> element.
In the configuration of the maven plugin you have already in place, you need to add the executable block like :
<configuration>
<executable>true</executable>
</configuration>
this will create in the target folder the runnable jar that can be run by using java -jar command.

mvn process-resources doesn't pull down uber jar created with shade plugin

Goal: Create an executable uber jar with maven shade plugin that can be executed during the mvn compile of another pom.
Repro steps:
Create a pom.xml for the "publisher" component using below pom.
Use a Jenkins build to mvn deploy it (mvn install will work as well)
Add dependency to pom.xml for "consumer" (pom below)
mvn compile the consumer
Expected behavior:
Uber jar for publisher is downloaded somewhere in consumer/target directories
Actual:
Uber jar does not appear in consumer directory
Component 1: Publisher
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mec.experiment</groupId>
<artifactId>publisher</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.6.Final</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestFile>src/main/resources/META-INF/MANIFEST.mf</manifestFile>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Component 2: Consumer
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mec.experiment</groupId>
<artifactId>consumer</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.mec.experiment</groupId>
<artifactId>publisher</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
The answer from the possible duplicate I linked to in the comments has a link to a dead example page. Here's a supplement for you. The plugin configuration would belong inside the consumer pom.
exec:java
Full name:
org.codehaus.mojo:exec-maven-plugin:1.5.0:java
Description:
Executes the supplied java class in the current VM with the enclosing project's dependencies as classpath.
Attributes:
Requires a Maven project to be executed.
Requires dependency resolution of artifacts in scope: test.
The goal is thread-safe and supports parallel builds.
Since version: 1.0.
See especially executableDependency for your use case. That looks like it will allow you to reference producer according to its group id and artifact instead of hard-coding a path.

Categories