Maven - Building a variable based on found files - maven

I have a Maven build and i'd like to build some property based on files found in the resources.
Concretely, I'd like to build a ${builtinLocales} variable at the "process-resources" phase based the resources found. I can then incorporate it in some application property file.
E.g. if 2 files "labels_en.properties" and "labels_de.properties" are found, that variable should return "en;de" or "labels_en;labels_de".
The ultimate goal is to present to the users the available languages without having to, at run time, parse the full jar for seek after "^labels_(\w+)\.properties$" files.
Anyway to do this ?

Based on the answers to Using maven replacer plugin to list files in a folder and Exporting Maven properties from Ant code, I came up with this solution:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>list-locales</id>
<phase>generate-resources</phase>
<configuration>
<target>
<fileset id="my-fileset" dir="src/main/resources">
<filename regex="[^/]+_.+.properties"/>
</fileset>
<pathconvert targetos="unix" pathsep=";"
property="project.locales" refid="my-fileset">
<map from="${basedir}\src\main\resources\" to="" />
<regexpmapper from="[^/]+?_(.+).properties" to="\1"/>
</pathconvert>
</target>
<exportAntProperties>true</exportAntProperties>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>ant-contrib</groupId>
<artifactId>ant-contrib</artifactId>
<version>1.0b3</version>
<exclusions>
<exclusion>
<groupId>ant</groupId>
<artifactId>ant</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</plugin>
The solution relies on :
Building a Ant property "project.locales" based on the files found
Exposing this Ant property to Maven with <exportAntProperties>true</exportAntProperties>
Using the the Maven property ${project.locales} in any file where required.

Related

Integrate JQAssistant-Maven-Plugin with database analysis via RDBMS Plugin

I´m trying to move my existing software-analysis with jQAssistant (which is basically all steps and scripts in a textfile and executed manually) to the maven-plugin of jQAssistant in order to be able to perform the software-analysis at the build-server.
One step of the analysis is to get information about the database schema. This is done with the RDBMS-Plugin of jQAssistant.
But when i try to perform the analysis with maven the information about the database-connection seems to be lost and no database-analysis is performed at all.
The current code of the plugin-configuration looks like this:
<plugin>
<groupId>com.buschmais.jqassistant</groupId>
<artifactId>jqassistant-maven-plugin</artifactId>
<version>1.8.0</version>
<executions>
<execution>
<id>cli-default</id>
<goals>
<goal>scan</goal>
<goal>analyze</goal>
</goals>
<configuration>
<!-- Scan -->
<scanIncludes>
<scanInclude>
<url>jdbc:oracle:thin:username/password#host:1521:sid</url>
<scope>rdbms:connection</scope>
</scanInclude>
</scanIncludes>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc8</artifactId>
<version>12.2.0.1.0</version>
</dependency>
</dependencies>
</plugin>
I couldn´t find anything in the documentation. Is this a limitation of the jqassistant-maven-plugin or is this a configuration error?

Wrapping Ant in Maven - JAVA_HOME points to the JRE but works with just Ant

I have an Ant project that builds just fine on its own. I'm now trying to wrap it in a Maven build that will kick off the Ant build using maven-antrun-plugin. When I do this the build fails and I get this error,
[ERROR] C:\Users\bobby\workspace\libraries\build-targets\common-targets.xml:170: Unable to find a javac compiler;
[ERROR] com.sun.tools.javac.Main is not on the classpath.
[ERROR] Perhaps JAVA_HOME does not point to the JDK.
[ERROR] It is currently set to "C:\Java\jdk1.8.0_65\jre"
There are a lot of SOF posts on this error but I feel like mine is unique since it only happens when I'm wrapping the Ant build in Maven i.e., I do not get this error on the same project when I just say $ ant build.
This is part of my pom.xml file
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>process-resources</phase>
<configuration>
<tasks>
<ant antfile="build.xml" target="build" />
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>add-jar</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>build/bin/myWarFile.war</file>
<type>war</type>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
My JAVA_HOME Environment Variable is set to C:\Java\jdk1.8.0_65.
The file that is mentioned in the error is from a library my work maintains where we keep all of our Jars. In that file here is what's on line 170
<target name="compile-src">
<!-- Compile source -->
<javac srcdir="${java.src.dir}"
destdir="${class.dir}"
debug="${debug.flag}"
deprecation="${deprecation.flag}"
nowarn="${warnings.flag}"
optimize="off"
source="${source.value}">
<classpath refid="compile.classpath"/>
</javac>
</target>
The line with source= is line 170.
It's a common issue. Try with this configuration:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
...
<!-- Add this dependency to your ant-run configuration -->
<dependencies>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.5.0</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>
...
</plugin>
Maven uses Java's system property java.home, which is not the same as the environment variable JAVA_HOME, but it is using it to compute its java.home by tacking on the jre sub-directory, as witnessed. Consequently, stuff needed by Ant is simply not available in the jre directory.
However, this configuration ensures that Ant's plugin dependencies are correctly satisfied.
You need to point to JDK not JRE. Just remove ire and try.
It is currently set to "C:\Java\jdk1.8.0_65\jre"
And if your JDK is set - another workaround - Can you copy tools.jar from jdk lib to jre lib and see if it works.

How can I compile and run my Custom Doclet class in my project?

I'm trying to dump all class javadoc comment (preferabbly subclasses of a libraries WebPage class) at compile time into a .properties file in the format classname=comment.
So far I have:
created doclet class SiteMapDoclet
the class is defined to scan all the javadocs in the project and dump them to a .properties file
Added the necessary configs to my pom.xml for it to work.
Versions: Java 1.6.0.21, Maven 2.2.1
Problem:
mvn site returns:
Embedded error: Error rendering Maven report:
Exit code: 1 - java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at us.ak.state.revenue.cssd.Personnel.utils.SiteMapDoclet.<clinit>(SiteMapDoclet.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
I tried setting the jars as AdditionalDependencies even though they are normal dependencies for my project.
I also tried adding the paths to the jars I expect my class to need as part of the bootclasspath.
the reporting section of my pom.xml looks like this:
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9</version>
<reportSets>
<reportSet>
<id>html</id>
<reports>
<report>javadoc</report>
</reports>
</reportSet>
<reportSet>
<id>siteMap</id>
<configuration>
<doclet>
us.ak.state.revenue.cssd.Personnel.utils.SiteMapDoclet
</doclet>
<docletPath>${project.build.outputDirectory}</docletPath>
<destDir>SiteMap</destDir>
<author>false</author>
<useStandardDocletOptions>false</useStandardDocletOptions>
<!-- there has got to be a better way to do this! -->
<!-- how can I fix the CSSD-Web - Base to use a proper manifest file? -->
<bootclasspath>
${bootClassPath};
${env.CLASSPATH};
${m2Repository}/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar;
${m2Repository}/org/apache/wicket/wicket-core/${wicket.version}/wicket-core-${wicket.version}.jar;
${m2Repository}/us/ak/state/revenue/cssd/CSSD-Web/${CSSDWebBase.version}/CSSD-Web-${CSSDWebBase.version}.jar
</bootclasspath>
<additionalDependencies>
<additionalDependency>
<groupId>us.ak.state.revenue.cssd</groupId>
<artifactId>CSSD-Web</artifactId>
<version>${CSSDWebBase.version}</version>
</additionalDependency>
<additionalDependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket-core</artifactId>
<version>${wicket.version}</version>
</additionalDependency>
<additionalDependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</additionalDependency>
<additionalDependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</additionalDependency>
</additionalDependencies>
<name>SiteMapDoclet</name>
<description>Page Descriptions for SiteMap generation</description>
</configuration>
<reports>
<report>javadoc</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
NOTE:
${m2Repository} is defined as a property higher up the file,
defined as ${env.USERPROFILE}/.m2/repository
${bootClassPath} is defined as a property higher up the file,
defined as ${env.JRE_6_HOME}\lib\rt.jar;${env.JAVA_HOME}\lib\tools.jar;
How can i fix the NoClassDefFoundError?
Additionally I would like my SiteMap file to run as a part of the normal build process,
after compile but before package.
I've tried defining this in build, but the javadoc doesn't get created and I don't see any logging output from my Doclet.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9</version>
<executions>
<execution>
<id>build-siteMap-Descriptions</id>
<phase>process-classes</phase>
</execution>
</executions>
</plugin>
UPDATE:
Thanks to #ben75 's suggestion. I've removed the <reporting> section of my pom.xml and now have the process failing during build. I added <goals> and copied the <configuration> section from <reporting>.
It's still throwing the NoClassDefFoundError but it's happening on build where I want it to. I tried adding:
<includeDependencySources>true</includeDependencySources>
<dependencySourceIncludes>
<dependencySourceInclude>org.apache.wicket:wicket-core:*</dependencySourceInclude>
<dependencySourceInclude>org.apache.commons.logging:*</dependencySourceInclude>
<dependencySourceInclude>us.ak.state.revenue.cssd:CSSD-Web:*</dependencySourceInclude>
</dependencySourceIncludes>
To the configuration section, but that didn't work.
You can try to put your <additionalDependencies> as plugin dependendencies:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9</version>
<dependencies>
<dependency>
<groupId>us.ak.state.revenue.cssd</groupId>
<artifactId>CSSD-Web</artifactId>
<version>${CSSDWebBase.version}</version>
</dependency>
...
To attach javadoc plugin to your normal build process, I think you just need to specify the goal and preferably attaching it to prepare-package phase (so that javadoc is not generated when you simply run the test phase):
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9</version>
<executions>
<execution>
<id>attach-javadoc</id>
<phase>prepare-package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Building upon #ben75s Excellent Advice I was able to get it to finally run.
This "works". It feels wrong and I'd love to see a better method.
Here's what I did:
Defined the plugin in the build section with the javadoc goal.
made sure tools.jar and rt.jar are in the <bootclasspath>
defined the <docletPath> as \;.;${project.build.outputDirectory};
the \;.; are necessary because maven doesn't append correctly
also had to explicitly add the path to some of the packages here to prevent inheritence of conflicting versions. (Specifically of Log4J)
defined <docletArtifacts> with the packages for the classes throwing the NoClassDefFoundError
My plugin now looks like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9</version>
<executions>
<execution>
<id>build-siteMap-Descriptions</id>
<phase>process-classes</phase>
<goals>
<!--<goal>aggregate</goal>-->
<goal>javadoc</goal>
</goals>
<configuration>
<doclet>
us.ak.state.revenue.cssd.Personnel.utils.SiteMapDoclet
</doclet>
<!-- the initial '\;.;' is required
because maven doesn't separate the path statements properly
The 5 packages are necessary
because otherwise slf4j complains about multiple bindings
-->
<docletPath>
\;.;${project.build.outputDirectory};
${m2Repository}/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar;
${m2Repository}/log4j/log4j/1.2.16/log4j-1.2.16.jar;
${m2Repository}/log4j/apache-log4j-extras/1.1/apache-log4j-extras-1.1.jar;
${m2Repository}/us/ak/state/revenue/cssd/CSSD-Web/${CSSDWebBase.version}/CSSD-Web-${CSSDWebBase.version}.jar;
${m2Repository}/org/apache/wicket/wicket-core/${wicket.version}/wicket-core-${wicket.version}.jar;
${m2Repository}/org/apache/wicket/wicket-util/${wicket.version}/wicket-util-${wicket.version}.jar;
</docletPath>
<docletArtifacts>
<!--
<docletArtifact>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</docletArtifact>
-->
<docletArtifact>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.2</version>
</docletArtifact>
<!-- how do I fix the download errors? -->
<!--
<docletArtifact>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.2</version>
</docletArtifact>
-->
<!--
<artifact>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</artifact>
-->
<!--
<docletArtifact>
<groupId>log4j</groupId>
<artifactId>apache-log4j-extras</artifactId>
<version>1.1</version>
</docletArtifact>
<docletArtifact>
<groupId>us.ak.state.revenue.cssd</groupId>
<artifactId>CSSD-Web</artifactId>
<version>${CSSDWebBase.version}</version>
</docletArtifact>
<docletArtifact>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket-core</artifactId>
<version>${wicket.version}</version>
</docletArtifact>
-->
</docletArtifacts>
<!-- the initial '\;.;' is required
because maven doesn't separate the path statements properly -->
<bootclasspath>
\;.;
${bootClassPath};
${env.CLASSPATH};
</bootclasspath>
<destDir>SiteMap</destDir>
<author>false</author>
<!-- don't print the packages/classes it's running on -->
<quiet>true</quiet>
<debug>true</debug> <!-- save options -->
<useStandardDocletOptions>false</useStandardDocletOptions>
<name>SiteMapDoclet</name>
<description>Page Descriptions for SiteMap generation</description>
</configuration>
</execution>
</executions>
</plugin>

How to get a list of all dependency JARs in a parameter?

I'm working on a plugin for Talend Open Studio; the component architecture of that platform needs that all external JARs are declared in a component-descriptor XML file in a form like:
<IMPORT MODULE="commons-collections-3.2.1.jar" NAME="commons-collections-3.2.1"
REQUIRED="true"/>
I use the Maven dependency plugins to manage all these external JARs
Is there a way to get all the dependency names in a list or something? This way can I be able to build the required strings (using an antcontrib task, perhaps), fill a ${parameter} and finally add it to XML file using maven-replacer-plugin?
The simplest solution is to use the maven-dependency-plugin via the buld-classpath goal. This goal can be given supplemental parameters to put the result into a file like:
mvn dependency:build-classpath -Dmdep.outputFile=classpath.out
Ok, I partly resolved this way that should works with some limitations:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<dependencies>
<dependency>
<groupId>ant-contrib</groupId>
<artifactId>ant-contrib</artifactId>
<version>1.0b3</version>
<exclusions>
<exclusion>
<groupId>ant</groupId>
<artifactId>ant</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>ant</groupId>
<artifactId>ant-nodeps</artifactId>
<version>1.6.5</version>
</dependency>
</dependencies>
<executions>
<execution>
<phase>package</phase>
<id>copy-resources</id>
<configuration>
<exportAntProperties>true</exportAntProperties>
<tasks>
<!-- add the ant tasks from ant-contrib -->
<taskdef resource="net/sf/antcontrib/antlib.xml" classpathref="maven.plugin.classpath"/>
<var name="import.set" value=""/>
<for param="file">
<path>
<fileset dir="${project.build.directory}" includes="*.jar"/>
</path>
<sequential>
<var name="basename" unset="true"/>
<basename file="#{file}" property="basename"/>
<var name="filenames" value="${basename}"/>
<var name="import.clause" value='<IMPORT MODULE="${filenames}" NAME="${filenames}" REQUIRED="true"/>'/>
<var name="import.set" value="${import.clause}${line.separator}${import.set}" />
</sequential>
</for>
<property name="import.jar" value="${import.set}"/>
<echo>${import.jar}</echo>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
Still some problems: even if exportAntProperties is set to true, the property ${import.jar} is still not available outside ant taska in other maven goals, while if i switch to maven-antrun-plugin 1.7 version, a "Error executing ant tasks: org.apache.tools.ant.launch.Locator.fromJarURI(Ljava/lang/String;)Ljava/lang/String;" exception is thrown. Still no clues...

docbkx-maven-plugin: No output during run of plugin

I set up the docbkx-maven-plugin to generate PDF documentation for a source project. In the parent pom I specified the version to be used as well as the reference to the docbook version to use:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.agilejava.docbkx</groupId>
<artifactId>docbkx-maven-plugin</artifactId>
<version>2.0.14</version>
<dependencies>
<dependency>
<groupId>net.sf.docbook</groupId>
<artifactId>docbook-xml</artifactId>
<version>5.0-all</version>
<type>zip</type>
<classifier>resources</classifier>
<scope>runtime</scope>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</pluginManagement>
In the actual project I use the configuration:
<build>
<plugins>
<plugin>
<groupId>com.agilejava.docbkx</groupId>
<artifactId>docbkx-maven-plugin</artifactId>
<executions>
<execution>
<id>usersmanual</id>
<phase>generate-resources</phase>
<goals>
<goal>generate-pdf</goal>
</goals>
<configuration>
<includes>${basedir}/UserManual/*.xml</includes>
<includes>${basedir}/UserManual/src/*.xml</includes>
<targetDirectory>${project.build.directory}/UserManual</targetDirectory>
<chunkedOutput>true</chunkedOutput>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
No matter what goal(s) I specify or what includes I provide, the plugin performs no(!) operation. There is no target directory created and I do not see any meaningful output on the command line. The result looks like:
[INFO] --- docbkx-maven-plugin:2.0.14:generate-pdf (usersmanual) # documentation ---
[INFO]
I use Maven 3.0.3.
What do I miss here? Is there any configuration not provided, yet, which will start the plugin to do some work?
UPDATE:
This is what I have now:
<plugin>
<groupId>com.agilejava.docbkx</groupId>
<artifactId>docbkx-maven-plugin</artifactId>
<version>2.0.14</version>
<dependencies>
<dependency>
<groupId>net.sf.docbook</groupId>
<artifactId>docbook-xml</artifactId>
<version>5.0-all</version>
<type>zip</type>
<classifier>resources</classifier>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>generate-pdf</goal>
</goals>
<phase>prepare-package</phase>
<configuration>
<sourceDirectory>${project.basedir}/UserManual</sourceDirectory>
<xincludeSupported>true</xincludeSupported>
<includes>${project.basedir}/UserManual/UserManual.xml</includes>
<includes>${project.basedir}/UserManual/**/*.xml</includes>
<targetDirectory>${project.build.directory}/UserManual</targetDirectory>
</configuration>
</execution>
</executions>
</plugin>
At least the directory target/Usermanual is greated, but it is still empty. Why is there still not output? Do I have a chance to get a meaningful log file from docbkx? mvn ... -X does not tell much.
Your latest example contains two includes configuration options which is not valid maven configuration.
My recommendation is to stop trying to override all these defaults and accept the default location for the docbook source xml, at least initially while you get comfortable with the plugin and can diagnose what issues are from what changes.
Anyway, your <includes> should be just the root xml file of the documentation you're trying to generate as it exists in the <sourceDirectory>. You do not need to include all of the xml files, you instead need to follow the xincludes approach since you're declaring its usage. There are a number of projects using this mechanism that you can review and copy the usage of.
Ours is: https://github.com/jetty-project/jetty-documentation
We have a bit more complex usage since we use the maven filtering plugin to avoid having to mess with entities and the like. Getting back to your includes usage, if your top level docbook file is index.xml then your includes would simply be:
<includes>index.xml</includes>
No expressions or globs needed, you bring in the other xml documents with the <xi:include> tags where needed.

Resources