Include Maven Site HTML pages in JAR's resources - maven

I've got a Maven project that builds a JAR file.
It also creates a site (using maven-site-plugin) that describes information about the project.
I'd like to include the resultant html pages generated by the maven-site-plugin in the resources of the created JAR so that they can be accessed at runtime by a help system.
Is this possible? If so, how?
I've tried using the site:jar goal but this always creates an additional JAR with "-site" appended, as per the documentation.

I've solved this by using maven-resources-plugin to copy the site from the ${project.build.directory}/site directory to ${project.build.directory}/classes, which is the contents of the final JAR.
Example:
<?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.me</groupId>
<artifactId>site-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<site.directory>${project.build.directory}/site</site.directory>
</properties>
<build>
<resources>
<resource>
<directory>${site.directory}</directory>
</resource>
</resources>
<plugins>
<plugin>
<!-- Include the maven site in the final JAR - we do this by running site:site BEFORE install -->
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<resources>
<resource>
<directory>${site.directory}</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

Related

Maven War Plugin: Copy webResources after resources

I am using the maven-war-plugin to copy the webapp resources and a few external resources to the target directory and then build the war file.
Here is the plugin configuration,
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<!-- Importing React App which is not in the build path -->
<webResources>
<resource>
<directory>react/build</directory>
<targetPath>ui/build</targetPath>
</resource>
</webResources>
</configuration>
<executions>
<execution>
<id>default-war</id>
<phase>package</phase>
<goals>
<goal>war</goal>
</goals>
</execution>
</executions>
</plugin>
This configuration works well, but the external resources (webResources) are copied first and then the build path content is copied.
Build steps,
Copy external resources (webResources)
Copy resources from build path (warSourceDirectory)
Build War file.
This is an extract from the maven build logs,
[INFO] --- maven-war-plugin:2.2:war (default-war) # ReactProject ---
...
[INFO] Copying webapp webResources [...\react\build] to [...\target\ReactProject-0.0.1]
[DEBUG] + \ui\build\app.js has been copied.
...
[INFO] Copying webapp resources [...\ReactProject\src\main\webapp]
[DEBUG] + \ui\html\index.html has been copied.
...
[INFO] Building war: ...\ReactProject\target\ReactProject-0.0.1.war
Due to this behavior if similar content is present in the build path it overwrites the content copied from the react folder. Also, it would be more intuitive if the external content is copied after the build path resources are configured.
Is there a way to configure the webResources to be copied after the main resources?
Complete pom file
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>ReactProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>ReactProject</name>
<description>React Project</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
...
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<!-- Importing React App which is not in the build path -->
<webResources>
<resource>
<directory>react/build</directory>
<targetPath>ui/build</targetPath>
</resource>
</webResources>
</configuration>
<executions>
<execution>
<id>default-war</id>
<phase>package</phase>
<goals>
<goal>war</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

How to set values for properties tag of maven by reading the .properties file

I have 5 projects, here I want to copy the java classes from one non-maven project to maven project for this requirement I used maven-ant-plugin. with this I am able to do the copy successfully.
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>com.test</groupId>
<artifactId>Test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project1_maven.src.main.java>c:/maven1/src/main/java</project1_maven.src.main.java>
<project1_maven.src.test.java>c:/maven1/src/test/java</project1_maven.src.test.java>
<project1.nonmaven.src>c:/non-maven1/src</project1.nonmaven.src>
<project1.nonmaven.test>c:/non-maven1/test</project1.nonmaven.test>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>install</phase>
<configuration>
<target>
<copy file="${project1.nonmaven.src}"
todir="${project1_maven.src.main.java}" />
<copy file="${project1.nonmaven.test}"
todir="${project1_maven.src.test.java}" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
</build>
</project>
But here my question is, instead of giving the source and destination paths (I want to give this like for remaining 4 projects) inside the pom.xml directly, I just want to read it from the properties file.
for example :
<project1.nonmaven.src>
${get the location from property bundle by using key}</project1.nonmaven.src>
Can you please some one suggest me how ??
Thanks.

How to share a filtered resource at generate-sources phase in a multi module project?

I have a parent project with 3 child projects:
parent
project-1
/src/main/resources/config.xml
project-2
/src/main/resources/config.xml
project-3
/src/main/resources/config.xml
The configuration config.xml is used during the generate-sources phase. For the three projects, the config.xml is exactly the same. However, the usage of this config.xml is different for each project.
In project-X, I am referring to config.xml as following:
<build>
<plugins>
<plugin>
<groupId>some-group</groupId>
<artifactId>some-artifact</artifactId>
<executions>
<execution>
<goals>
<goal>some-goal</goal>
</goals>
<configuration>
<input>src/main/resources/config.xml</input>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
What is the best way to share this common config.xml between all 3 projects?
You can use the build-helper-maven-plugin here.
PROJECT STRUCTURE
shared-resources-project
+-src
+-main
+-resources
`config.xml
+-project-A
`pom.xml
+-project-B
`pom.xml
+-project-C
`pom.xml
`pom.xml
shared-resources-project/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>my</groupId>
<artifactId>shared-resources-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<modules>
<module>project-A</module>
<module>project-B</module>
<module>project-C</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.10</version>
<executions>
<execution>
<id>add-resource</id>
<phase>generate-sources</phase>
<goals>
<goal>add-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<filtering>true</filtering>
<directory>${project.parent.basedir}/src/main/resources</directory>
<includes>
<include>config.xml</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>some-group</groupId>
<artifactId>some-artifact</artifactId>
<executions>
<execution>
<id>some-plugin-job</id>
<phase>generate-sources</phase>
<goals>
<goal>some-goal</goal>
</goals>
<configuration>
<input>${project.build.outputDirectory}/config.xml</input>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
shared-resources-project/src/main/resources/config.xml
<config>
<parameter>${custom-value}</parameter>
</config>
project-X/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>
<parent>
<groupId>my</groupId>
<artifactId>shared-resources-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>project-X</artifactId>
<properties>
<custom-value>Project-X Value</custom-value>
</properties>
</project>
Now, let's build the project:
D:\workspaces> cd shared-resources-project
D:\workspaces\java\shared-resources-project> mvn clean install
Some notes:
The build-helper-maven-plugin will add the common config.xml file as a resource to Project-X.
Then the Maven resources plugin (MRP) will copy config.xml to the project output directory (target directory by default). During the copy, MRP will also replace ${custom-value} with the specific value provided by Project-X.
The final config.xml will be available to another plugin as long as the other plugin is bound to the generate-source phase AND its declaration appears AFTER the build-helper-maven-plugin declaration. Maven (3.0.4+ at least) calls the plugins in their order of apparition in the pom.xml.

Why my Maven project automatic generate apidoc in target folder?

I didn't configurated maven-javadoc-plugin in my maven project pom.xml file,but when I run mvn install command,then generate apidoc?
configuration of pom.xml as follow :
<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>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<build>
<finalName>webcnmobile</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<!-- web.xml is not mandatory since JavaEE 5 -->
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
</build>
the spring-security-oauth-parent pom.xml see here
The javadoc plugin is configured in the parent pom that you are referencing (spring-security-oauth-parent). This means that you inherit this configuration and it automatically gets applied to your project.
The configuration in the parent pom is as follows:
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>javadoc</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
This is then being applied to your project and hence you are getting javadoc generated.

maven-resources-plugin error using copy-resources goal: 'resources', 'outputDirectory' missing or invalid

I'm trying to use the maven-resources-plugin to do some filtering using the copy-resources goal, and ran into the following error:
Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:2.5:copy-resources (default-cli) on project bar: The parameters 'resources', 'outputDirectory' for goal org.apache.maven.plugins:maven-resources-plugin:2.5:copy-resources are missing or invalid
To isolate the problem, I created a very simple pom.xml, copied pretty nearly verbatim from http://maven.apache.org/plugins/maven-resources-plugin/examples/copy-resources.html, ran it, and got the same error.
I'm invoking it with
mvn resources:copy-resources
Any ideas? Here's the test pom.xml.
<?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>foo</groupId>
<artifactId>bar</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/extra-resources</outputDirectory>
<resources>
<resource>
<directory>src/non-packaged-resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
The main problem you had is that you are invoking the plugin goal directly using
mvn resources:copy-resources
which does not necessarily create the output directory. Instead call the correct Maven lifecycle phase.
mvn process-resources
For a complete list of the lifecycle phases just run the mvn command without anything..
In general it almost always better to invoke a lifecycle phase rather than a goal directly since it guarantees that any preconditions are met (e.g. cant compile test classes before the classes to be tested..).
Check if #bmargulies answer works for you. You can refer to these examples.
In any case, you do not need to use <pluginManagement> to achieve this. <pluginManagement> is used in multi-module scenarios to facilitate inheritance of plugin configurations.
You need to move the configuration out of execution element. The following snippet works.
<?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>foo</groupId>
<artifactId>bar</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<configuration>
<outputDirectory>${basedir}/target/extra-resources</outputDirectory>
<resources>
<resource>
<directory>src/non-packaged-resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Just remove the executions and their configuration. Normally you might want to define resources in <build> > <resources> respectively <build> > <testResources> (see http://maven.apache.org/plugins/maven-resources-plugin/examples/resource-directory.html) directly not within the plugin config using the default lifecycle process-(test-)resources which is automatically hooked by copy-(test-)resources..
Yes, it's a bad example on their page!

Resources