Maven Build Javadoc Zip during mvn javadoc:javadoc - java

I'm generating a ZIP of generated Javadoc during mvn install operation by using the following:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>docs-assembly</id>
<phase>package</phase>
<configuration>
<finalName>${project.build.finalName}-${project.version}-Javadoc</finalName>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/assemble.xml</descriptor>
</descriptors>
</configuration>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
But if I want to generate this ZIP file by launching mvn javadoc:javadoc, the ZIP file is not generated:
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>docs-assembly</id>
<phase>package</phase>
<configuration>
<finalName>${project.build.finalName}-${project.version}-Javadoc</finalName>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/assemble.xml</descriptor>
</descriptors>
</configuration>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
I Just copy / paste the execution but the docs-assembly is not executed with javadoc:javadoc...
why it is not called during javadoc:javadoc call ?

Your main question is: why is maven-assembly-plugin:single not called during javadoc:javadoc call?
For this you need to understand the difference between lifecycles and plugin goals.
A plugin goal is a single task, easy to recognize by the colon. So javadoc:javadoc is a plugin goal.
Consider a lifecycle as an ordered group of plugin goals. You can call a lifecycle-phase (has no colons) and Maven knows which plugins need to be executed. package is a lifecycle-phase.
So if you are calling javadoc:javadoc, that will be the only task. There's no reason for Maven to also execute maven-assembly-plugin:single.
btw, did you try javadoc:jar? You should know that jar is just a zip file with extras.

Related

How to package a jar and all dependencies within a new jar with maven

I have a maven project with like ten dependencies. Before, I used to pack all of that in a single jar thanks to maven-assembly-plugin:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>create-executable-jar</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
<archive>
<manifest>
<mainClass>myApp.Main</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
But now, I added a step before. I have a plugin that will generate the jar of my application. So I just want the assembly plugin to add the dependencies to this jar. Unfortunately, the plugin doesn't use this jar, but instead, seems tu be using the result from the compiler.
is there a way to specify that I want the plugin to use the previously generated jar instead of the result from the compiler ?
Try using the maven-shade-plugin. You need something like:
<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>

maven can't find generated sources (mvn clean install)

I've just got the problem some hours ago and it always seemd to working until now.
I generate code in my pom on the following way:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<configuration>
<sourceDestDir>${basedir}/target/generated/java</sourceDestDir>
<keep>true</keep>
<verbose>true</verbose>
<extension>true</extension>
<wsdlDirectory>${basedir}/src/main/resources/META-INF</wsdlDirectory>
</configuration>
<executions>
<execution>
<id>ecad-ws</id>
<phase>generate-sources</phase>
<goals>
<goal>wsimport</goal>
</goals>
<configuration>
<wsdlFiles>
<wsdlFile>wsdl/ECadDocumentServiceWSDL.wsdl</wsdlFile>
</wsdlFiles>
<staleFile>${project.build.directory}/jaxws/stale/wsdl.ECadDocumentServiceWSDL.done</staleFile>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaDirectory>${basedir}/src/main/resources/META-INF/xsd</schemaDirectory>
<packageName>be.fgov.health.ecad.xmlstructure</packageName>
<outputDirectory>${basedir}/target/generated/java</outputDirectory>
</configuration>
</plugin>
and I use those generated classes in my project.
If I then do a "right click -> maven -> clean" + "right click -> maven -> install" everything is working.
But when I run mvn clean install -DskipTest=true, then maven can't find the generated sources.. I'm stuck for several hours already and can't really find it. (doing this in Eclipse btw)
EDIT:
just figured out the following: If I remove the second plugin (to generate by xsd) I won't get any error.. If I put all the code that uses thoes generated classes in comment ofc.
Another EDIT:
I've changed the outputDirectory from the jaxb generation and now it's working. Can anyone explain me why it can't be the same as the wsimport location?
By default, the jaxb2-maven-plugin deletes the outputDirectory before putting the generated classes inside.
You can control this behaviour with the attribute clearOutputDir. Your plugin configuration would then look like :
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaDirectory>${basedir}/src/main/resources/META-INF/xsd</schemaDirectory>
<packageName>be.fgov.health.ecad.xmlstructure</packageName>
<outputDirectory>${basedir}/target/generated/java</outputDirectory>
<clearOutputDir>false</clearOutputDir>
</configuration>
</plugin>

Maven and JavaDoc: install additional (generated) files

My project can generate some additional help files automatically from the source code.
How do I have maven install these files into the generated JavaDoc package?
I couldn't fingure out how to have Maven:
run some class to generate the additional documentation, e.g. compile and launch src/main/java/mypackage/ListOptions.java aka mypackage.ListOptions to produce a list of all available options/modules/examples/etc. .
install the output files (I only could get Maven to install files from src/main/javadoc/resources into the site/apidocs/resources subfolder; but I want this documentation to live in the top level site/apidocs folder; more precisely I don't care about the site part at all, but about having a complete documetation in mypackage-0.1.0-SNAPSHOT-javadoc.jar; including non-Javadoc-generated auxillary information linked from javadoc).
Minimal example - Plugin configuration for maven:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
<configuration>
<docfilessubdirs>true</docfilessubdirs>
</configuration>
</plugin>
Directory layout:
./pom.xml
./src/main/java/foobar/GenerateNonstatic.java
./src/main/javadoc/staticfile.js
./src/main/javadoc/resources/insubfolder.png
mvn package produces: javadoc in ./target/apidocs and ./target/foobar-0.0.1-SNAPSHOT-javadoc.jar. The file target/apidocs/resources/insubfolder.png ends up in folder target/apidocs/resources (and the .jar), but the staticfile.js is not installed.
How to run GenerateNonstatic.java to have the output included in the javadoc .jar is unclear to me. I don't see a hook to have exec:exec it run after javadoc, and before ./target/foobar-0.0.1-SNAPSHOT-javadoc.jar is built...
Long story, short answer. Both the javadoc:javadoc and javadoc:jar mojos have their drawbacks. The first is meant to build the javadoc for reporting; the latter will build the javadoc (into a different directory) and produce a jar package.
Some of the suggested answers did not work well because of this - they would execute javadoc twice.
But I noticed that javadoc does not care if the folder already exists or contains files.
So my solution is simple: generate the desired additional files in prepare-package, and the regular javadoc:jar task adds the regular javadoc and pacakges it nicely.
This will work as long as you don't intend to modify files generated by javadoc (which I, fortunately, don't - I only add additional documentation.
The resulting pom.xml is like this:
<plugins>
<!-- copy additional javadoc resources -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/apidocs</outputDirectory>
<resources>
<resource>
<directory>${basedir}/src/main/javadoc</directory>
<excludes>
<!-- overview.html is integrated by javadoc -->
<exclude>${basedir}/src/main/javadoc/overview.html</exclude>
</excludes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<!-- Generate additional files for javadoc -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<executions>
<execution>
<id>generate-extra-javadoc</id>
<phase>prepare-package</phase>
<goals>
<!-- java is okay, but you only can have one -->
<goal>exec</goal>
</goals>
<configuration>...</configuration>
</execution>
</executions>
</plugin>
<!-- Build JavaDoc -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.1</version>
<executions>
<execution>
<id>attach-javadocs</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
Eric,
If you are uncomfortable modifying the javadoc executable, another option is to break apart your javadoc call into 2 separate steps (javadoc and jar) and make your call between them by manipulating the Maven build lifecycle via <phase> tag:
phase: generate-sources => maven-javadoc-plugin:javadoc
phase: compile => exec-maven-plugin:java
phase: package => maven-javadoc-plugin:jar
note: use exec:java, not exec:exec to run java classes
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<executions>
<execution>
<id>generate-javadocs</id>
<phase>generate-sources</phase>
<goals>
<goal>javadoc</goal>
</goals>
</execution>
<execution>
<id>attach-javadocs</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<executions>
<execution>
<id>generate-nonstatic-javadocs</id>
<phase>compile</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>foobar.GenerateNonstatic</mainClass>
</configuration>
</execution>
</executions>
</plugin>
To answer your first question: Run a commandline command with the Exec-Maven-Plugin or use the Maven-Ant-Plugin and embed custom tasks.
Eric,
Your example is indeed helpful. Please do try to always include concrete examples as much as possible when asking the question.
The simplest approach would be to write your own Alternate Javadoc tool and pass that into Maven via the <javadocExecutable> tag. This example comes from the link above:
<project>
...
<reporting> (or <build>)
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.1</version>
<configuration>
<javadocExecutable>C:\jdk1.6.0\bin\javadoc.exe</javadocExecutable>
...
</configuration>
</plugin>
...
</plugins>
</reporting> (or </build>)
...
</project>
Hope that helps.

wrong execution order when bound maven assembly plugin twice to package phase

I want to use proguard maven plugin to execute between 2 assembly descriptor (one of folder format and one of zip format) so I define my pom:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>raw-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<finalName>raw</finalName>
<descriptors>
<descriptor>src/assemble/raw.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<version>2.0.7</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>proguard</goal>
</goals>
</execution>
</executions>
<configuration>
<options>
<option>#${project.build.directory}/raw/proguard/proguard.cfg</option>
</options>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>distro-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<finalName>distro</finalName>
<descriptors>
<descriptor>src/assemble/distro.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
But the maven assembly plugin always executes 2 descriptor before proguard plugin.
I use Maven 3.2.2
Thanks,
When determining run order, Maven runs goals bound to earlier phases in the lifecycle first. If multiple goals are bound to the same phase, then they run in the order they are listed in the POM.
When you want to run a plugin more than once, you don't add the plugin twice as you've shown, you put multiple executions in a single plugin. If you want the proguard plugin to execute before the assemblies, you may bind the goals to an earlier phase (e.g. prepare-package). If the phase must be package for some reason, then the proguard plugin config must appear before the assembly plugin config in the file.
If you need the first assembly execution to run before the proguard plugin execution, and the second assembly execution after, then you need to use two phases. The proguard plugin config needs to appear in the file before the assembly plugin, and the execution should use phase package. The first assembly execution should use prepare-package, the second package.
This is your POM rearranged to work, with everything bound to the package phase as was shown in the original question.
<plugin>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<version>2.0.7</version>
<executions>
<execution>
<phase>package</phase> <!-- fixed spelling -->
<goals>
<goal>proguard</goal>
</goals>
</execution>
</executions>
<configuration>
<options>
<option>#${project.build.directory}/raw/proguard/proguard.cfg</option>
</options>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>raw-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<finalName>raw</finalName>
<descriptors>
<descriptor>src/assemble/raw.xml</descriptor>
</descriptors>
</configuration>
</execution>
<execution>
<id>distro-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<finalName>distro</finalName>
<descriptors>
<descriptor>src/assemble/distro.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
Package is spelled wrong in program
<plugin>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<version>2.0.7</version>
<executions>
<execution>
<phase>packge</phase> <--- Spelling Issue

Get Maven to run Javadoc:javadoc from mvn package

I have configure javadoc i Maven, but i have to execute mvn javadoc:javadoc
i like Maven to to this while i execute mvn package.
My pom look like
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>${maven-javadoc-plugin.version}</version>
<configuration>
<aggregate>true</aggregate>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<phase>prepare-package</phase>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
Your plugin configuration shows that the javadoc plugin is called in the phase prepare-package. Thus, if you call mvn package this plugin is executed. But the javadoc plugin has no goal package: see plugin info here.
I think you want to call the javadoc goal:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<configuration>
...
</configuration>
<executions>
<execution>
<id>generate-javadoc</id>
<phase>package</phase>
<goals>
<goal>javadoc</goal>
</goals>
</execution>
</executions>
</plugin>

Categories