Ascii-Doc Maven Plugin (include resources via https) - java

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.

Related

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?

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

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.

How do you replace the class of a Maven dependency?

There is a class in a maven dependency that is incompatible with Java 8.
How do you properly fix that problem?
Right now I'm doing the following:
Create a package with the same name
Create a class with the same name in that package
Copy and paste the code
Fix the incompatible API call
The problem is that this class contains API calls to restricted classes and although I changed the Eclipse compiler settings (Window -> Preferences -> Java -> Compiler -> Error/Warnings -> Deprecated and restricted API -> Forbidden reference (access rule): Error -> Warning) to allow access the project will only compile sometimes. If it doesn't compile I'll get a "can't find symbol" error.
Edit:
Here are the details you asked for:
Dependency: http://mvnrepository.com/artifact/com.sun.xml.wss/xws-security/3.0
Class: EncryptionProcessor
Necessary change:
// Change line 1053 FROM:
// _dataEncryptor = XMLCipher.getInstance(dataEncAlgo, _dataCipher);
// TO:
_dataEncryptor = XMLCipher.getInstance(dataEncAlgo);
Edit-2:
Maven build error:
[ERROR] symbol: class XMLCipher
[ERROR] location: class com.sun.xml.wss.impl.apachecrypto.EncryptionProcessor
[ERROR] /C:/Users/{name}/development/eclipse_workspace/git/xws-security/src/main/java/com/sun/xml/wss/impl/apachecrypto/EncryptionProcessor.java:[1482,98] cannot find symbol
Here is a detailed guide describing what I did exactly:
Create new Maven project in Eclipse
Configure Maven settings of new project (Important: Use the same group and artifact ID and only change the version number)
<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.sun.xml.wss</groupId>
<artifactId>xws-security</artifactId>
<version>3.0-java8-fix</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
</project>
Add dependency of bugged JAR
<dependencies>
<dependency>
<groupId>com.sun.xml.wss</groupId>
<artifactId>xws-security</artifactId>
<version>3.0</version>
<exclusions>
<exclusion>
<artifactId>xmldsig</artifactId>
<groupId>javax.xml.crypto</groupId>
</exclusion>
<exclusion>
<artifactId>activation</artifactId>
<groupId>javax.activation</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
Create Java file in the same package of class that needs to be fixed
package com.sun.xml.wss.impl.apachecrypto;
public class EncryptionProcessor {
// The FIX goes here
}
Add Maven shade build plug in to handle creation of patched JAR file (this is not the only plug in to handle this kind of task - e.g. dependency:unpack)
<build>
<plugins>
<!-- plug in for creation of patched JAR file -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>com.sun.xml.wss:xws-security:3.0</artifact>
<includes>
<include>**/*.class</include>
<include>**/*.xml</include>
</includes>
<excludes>
<exlude>
com/sun/xml/wss/impl/apachecrypto/EncryptionProcessor.class
</exlude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Include patched JAR in other projects as necessary (Note: If you experience ClassNotFoundExceptions or similar errors do this: Right-click on the project -> Properties -> Maven -> "Resolve dependencies from Workspace projects":false)
In case you are not familiar with Maven. Here is the complete pom.xml: http://pastebucket.com/88444
Similar to Steven S.'s answer, but using the maven-dependency-plugin. Based on this blog post.
I changed the name of the patched library (not the version), but it depends on your needs what works better for you.
The dependency on the original library should be marked as <optional>true</optional>. Otherwise, the projects that depend on your patched library will also depend on the original library, which means that both the patched and the original version will be on the classpath, which can lead to all kinds of problems.
If your project is a child project, you can still use a completely different groupId and version than your parent pom. Doesn't matter.
You can exclude the classes you patch from unpacking, but it's probably not necessary, because Maven will first unpack the original library and then compile your new version, which means that the original classes are overwritten. Nice!
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<!-- remove this if you don't have a parent pom -->
<parent>
<groupId>my.company</groupId>
<artifactId>my.company</artifactId>
<version>1.2.3</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>com.foo</groupId>
<artifactId>foo-bar-patched</artifactId>
<version>4.5.6</version>
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.foo</groupId>
<artifactId>foo-bar</artifactId>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<!-- excludes are probably not necessary -->
<!-- <excludes>**/Foo.class,**/Bar.class</excludes> -->
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.foo</groupId>
<artifactId>foo-bar</artifactId>
<version>4.5.6</version>
<optional>true</optional>
</dependency>
</dependencies>
</project>
General solution:
download all project sources
apply your modification
use version control so that change isn't lost
change version in pom.xml, for example from 3.0 to 3.0-patched
launch maven build
copy generated artifacts to you repository/Artifactory, if you use one
change dependency version in your own project

Can't resolve class dependencies in a MAVEN project with Child/Modules (seen in Eclipse and mnv CLI)

I have an Eclipse Maven Project (parent) that hosts three Maven MODULES (it's children). Only the child MODULES have code under "src/main/java/..." (i.e. the PARENT is just a stub place holder for the children).
Each MODULE is independent of one another... I just set it up that way to reduce clutter. =:)
Now the project structure didn't start out this way. Initially it was just one big PARENT and no child MODULES; and everything worked fine. But then I reorganized things within Eclipse (again to reduce clutter) using various moves/refactors, and things stopped working.
The Problem: My source code can't find imported classes now, so my dependency resolution became problematic somewhere. And the problem isn't just seen in Eclipse, but also when I run, say, 'mvn clean install' from the CLI. So I suspect something is wrong with the set of POM files that resulted from my moves/refactors.
Here they are (the PARENT and one CHILD). Am I missing something, or is something incorrect? Maybe I should check something Eclipse, too?
Note that I embedded a couple of little in-line questions inside the POM files below. :)
The PARENT 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>
<name>someName</name>
<packaging>pom</packaging>
<groupId>someParentGroupId</groupId>
<artifactId>someParentArtifactId</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<jdk.version>1.7</jdk.version>
<example-core.version>0.9.3</example-core.version>
<!-- Intended to be used by some Child/Module.
I hope PARENT/CHILD POM inheritance works that way? -->
</properties>
<dependencies>
<!-- Dependencies are all in the Children/Modules -->
</dependencies>
<!-- ################### BUILD SETTINGS BEGIN ##################### -->
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin> <!-- Used to create an UBER/FAT-JAR. -->
<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>
<configuration>
<finalName>uber-${project.artifactId}-${project.version}</finalName>
</configuration>
</plugin>
</plugins>
</build>
<!-- ################### BUILD SETTINGS END ####################### -->
<modules>
<module>childModule1</module>
<module>childModule2</module>
<module>chileModule3</module>
</modules>
</project>
Example CHILD 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>
<parent>
<groupId>someParentGroupId</groupId>
<artifactId>someParentArtifactId</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>someChildArtifactId</artifactId>
<!-- I noticed this missing in the Children/Module POM. Is that okay?
<name>
</name>
-->
<dependencies>
<dependency>
<groupId>org.example.somgGroupId</groupId>
<artifactId>example-core</artifactId>
<version>${example-core.version}</version>
</dependency>
</dependencies>
</project>
Again, the problem seems simple. I can't resolve imports (so, naturally, my Classes have import-ralated errors).
Thank you in advance!
name is not a mandatory attribute in pom.xml.
I assume someChildArtifactId is given as an example. If not, the same artifact id should be used in the parent pom.

Datanucleus JDO in maven assembly jar

I used the maven assembly package to create a single monolithic jar. Included is the datanucleus package, but I am getting errors because I did not maintain the OSGI structure (plugins.xml & META-INF/META-INF.MD). The answers on stackoverflow([question]: Datanucleus, JDO and executable jar - how to do it?) do not provide answers on how to create the single deployable jar.
Does anyone know what maven directives I can use to ensure OSGI structure?
So, the problem is that packing everything into a monolithic jar is that the OSGI structures get overritten by the OSGI structure of other jars. The benefit of the monolithic jar was to have a single file to push to the server but not necessary (since I was using capistrano to push to the server)
Instead, it's easier to just copy the jars into the final build directory. Heroku's Java setup has a perfect example of this:
https://devcenter.heroku.com/articles/java
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<version>1.0-SNAPSHOT</version>
<artifactId>helloworld</artifactId>
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>7.6.0.v20120127</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals><goal>copy-dependencies</goal></goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

Categories