How to eliminate org/apache/jsp from the output path of the Apache Sling jspc-maven-plugin? - maven

I am trying to use the jspc-maven-plugin from Apache Sling. When I compile my JSPs, they end up in my target/org/apache/jsp directory. I want them to go go directly under target or under target/classes w/o the org/apache/jsp part. How can I achieve this? Here is my plugin specification:
<plugin>
<groupId>org.apache.sling</groupId>
<artifactId>jspc-maven-plugin</artifactId>
<executions>
<execution>
<id>compile-jsp</id>
<goals>
<goal>jspc</goal>
</goals>
<configuration>
<sourceDirectory>${basedir}/source/html</sourceDirectory>
<outputDirectory>${basedir}/target</outputDirectory>
<includes>
<include>**/*.jsp</include>
</includes>
<showSuccess>true</showSuccess>
<failOnError>false</failOnError>
<compilerSourceVM>${javac.source}</compilerSourceVM>
<compilerTargetVM>${javac.target}</compilerTargetVM>
<servletPackage/> <!-- doesn't seem to matter - deprecated -->
</configuration>
</execution>
</executions>
</plugin>

Related

How to skip version in jar generated by maven-jar-plugin and clasifier

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>client</id>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>client</classifier>
<includes>
<include>**/com.service/**</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
I want the version to be appended to end of the jar.Right now it is building jar like ship-service-0.1-client.jar,I want it like ship-service-client-0.1.jar and i need to add it as dependency in another project.
You can use
<build>
<finalName>FinalNameOfYourProyect</finalName>
</build>
check this related question

maven-replacer-plugin to replace tokens in build and not source

I am trying to use the maven-replacer-plugin to replace tokens in my web.xml when it is built in the WAR file but not in the source, which would remove the tokens for subsequent builds and show the file as changed relative to the version control repository.
Currently, I am only able to change the file in the source, which does not meet my requirement:
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.2</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<file>${project.basedir}/src/main/webapp/WEB-INF/web.xml</file>
<replacements>
<replacement>
<token>##sec.level##</token>
<value>local</value>
</replacement>
</replacements>
</configuration>
</plugin>
Question: How can I run the replacer to only change the file in the WAR package while leaving the source unchanged for subsequent builds?
You can use the exploded goal of the maven-war-plugin to get to a temporary folder (like whatever created under target actually) the exploded version of what would later on be part of the final war file, then execute the replacer plugin on this file (a safe copy, not in conflict with other plugins consuming the file).
This approach is actually also documented by the official replacer plugin doc
That is, having a similar configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<useCache>true</useCache>
</configuration>
<executions>
<execution>
<id>prepare-war</id>
<phase>prepare-package</phase>
<goals>
<goal>exploded</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.2</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<file>${project.build.directory}/${project.build.finalName}/WEB-INF/web.xml</file>
<token>##sec.level##</token>
<value>local</value>
</configuration>
</plugin>
Note: the replacer documentation also suggests to use the useCache option which should prevent the plugin to override what the exploded goal previously created. However, the option doesn't really suit this purpose.
Similarly, the following approach would instead work according to my tests:
Use the exploded goal of the maven-war-plugin to create a temporary copy of the future war file in a <war_name>-tmp directory under target: that's not an issue, whatever is under target is supposed to be discarded via a clean command anyway
Configure the replacer plugin to replace that copy of the web.xml file
Configure the default war goal using its webXml option to point to that web.xml file for its final war file
The following would apply the approach described above:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<!-- explode the future war content for pre-package processing -->
<id>prepare-war</id>
<phase>prepare-package</phase>
<goals>
<goal>exploded</goal>
</goals>
<configuration>
<webappDirectory>${project.build.directory}/${project.build.finalName}-tmp</webappDirectory>
</configuration>
</execution>
<execution>
<!-- use the same execution id to further configure the default binding and execution -->
<id>default-war</id>
<phase>package</phase>
<goals>
<goal>war</goal>
</goals>
<configuration>
<!-- during the package phase, use the processed web.xml file -->
<webXml>${project.build.directory}/${project.build.finalName}-tmp/WEB-INF/web.xml</webXml>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.2</version>
<executions>
<execution>
<!-- apply pre-package processing on web resources -->
<id>process-web-resources</id>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<file>${project.build.directory}/${project.build.finalName}-tmp/WEB-INF/web.xml</file>
<token>##test##</token>
<value>local</value>
</configuration>
</plugin>

Generated sources being compiled twice

Using Eclipse Luna with m2eclipse, I have a parent Maven project (facturas_root) and two Maven modules inheriting from it (sharepoint_ws and api_sharepoint).
sharepoint_ws was to be used only to generate JAXWS classes to connect to the Sharepoint WebServices, so I downloaded the related WSDL and included those as resources of the project. At generate-sources phase, it works correctly and generates the sources in target\generated-sources\ws-consume\mypackage\.
Now, the issue is that I made api_sharepoint import the sharepoint_ws dependency, but it does not detect any class. I assumed that it was because the generated classes were not at src/main/java, so I added a plugin to copy them there. Now, the problem is that at the compile phase of sharepoint_ws, it finds twice the source file of each class and throws an error.
My pom.xml -> build
<plugins>
<!-- clean /src/main/java and /target/generated-sources -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.6.1</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>clean</goal>
</goals>
<configuration>
<filesets>
<fileset>
<directory>${basedir}/src/main/java/es</directory>
</fileset>
<fileset>
<directory>${basedir}/target/generated-sources</directory>
</fileset>
</configuration>
</execution>
</executions>
</plugin>
<!-- generate jaxws -->
<plugin>
<groupId>org.jboss.ws.plugins</groupId>
<artifactId>maven-jaxws-tools-plugin</artifactId>
<version>1.1.2.Final</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>wsconsume</goal>
</goals>
<configuration>
<wsdls>
<wsdl>${basedir}/resources/lists.wsdl</wsdl>
</wsdls>
<targetPackage>es.ssib.otic.facturas.sharepoint_ws</targetPackage>
<outputDirectory>${basedir}/target/generated-sources</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!-- copy sources -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-filtering</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/src/main/java</outputDirectory>
<resources>
<resource>
<directory>${basedir}/target/generated-sources/wsconsume</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
In order to try to exclude target/generated-sources I have addded this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
<phase>compile</phase>
<configuration>
<excludes>
<exclude>**/target/generated-sources/*.java</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
As stated above, I do comment the "copy" plugin, the module depending on sharepoint_ws does not have any ot its classes available; I do use it I get errors in the tune of
[ERROR] /C:/Users/s004256/workspace/facturas_root/sharepoint_ws/src/main/java/es/ssib/otic/facturas/sharepoint_ws/DeleteList.java:[34,8] duplicate class: es.ssib.otic.facturas.sharepoint_ws.DeleteList
for each generated list.
In the first place, I recommend you'd better declare target/generated-sources as a source folder, instead of copying files here and there:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>add-source</id>
<phase>initialize</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
This should be enough to make Maven compile the target/generated-sources/*.java and package them all in the library, and also for Eclipse to recognize target/generated-sources as a source directory (after you execute Maven/Update Project).
By the way: You should take care of binding the plugins to a phase in the correct order: If you bound all tasks to "generate-sources", you have no gurantee about in which order will they be executed. And the same goes for the "compile" phase: You have to set properly the source folders, with its inclusions and exclusions, before the compile phase.
Take a look a the Default Maven Lifecycle and try to chose different, sequential phases for your tasks.

Creating directory structure for application with maven

How can I tell maven to create a particular directory structure and to put jar files in certain places?
For example I want to put put some jars in a directory called plugins which are loaded at runtime.
Is that possible? Or am I missing the principles behind maven?
It looks like your jars is your project dependency, so to copy all dependencies at plugins folder use next:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-alldeps</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/plugins</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<excludeTransitive>false</excludeTransitive>
</configuration>
</execution>
</executions>
</plugin>
When your project use jars like resources, use maven resource section or next plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-config-files</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/plugins/</outputDirectory>
<resources>
<resource>
<filtering>false</filtering>
<directory>${project.basedir}/jars/</directory>
<includes>
<include>file1.jar</include>
<include>file5.jar</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

Rename directory while building war-file with Maven

Question
Overall Goal
I want to build a Java WebApp (war file) where folders containing static content have a version number append to it for cachability. Renaming should happen automatically during build.
Details
The WebApp is already structured in a way that all static content is grouped together inside folders:
/lib -> All JavaScript libraries, eg /lib/angular-1.0.3 (source at src/main/webapp)
/app -> My Own Code, at the moment /app/myApp (source at src/main/webapp)
/api -> all dynamic servlets (source at src/main/java)
index.jsp -> host page that bootstraps the WebApp
Everything starting with /api and index.jsp is dynamic and not cachable at all.
Everything starting with /lib is static and versioned, thus we can set an expires header of 'access + 12 month'. Content changes require a new version number. This is easy with libraries, as they don't change much an the version number is hardcoded.
Since my own app (built with AngularJS) is also static, I want to set its expires header to 'access + 12 month', too. To do so, I need to add the current version number from Maven to the end of the folder name, thus turning /app/myApp from the source directory to /app/myApp-1.2.3 in the target directory / final war file.
POM File
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<version>1.3.2</version>
<executions>
<execution>
<goals>
<goal>jslint</goal>
<goal>compress</goal>
</goals>
</execution>
</executions>
<configuration>
<excludeResources>true</excludeResources>
<removeIncluded>true</removeIncluded>
<suffix>.min</suffix>
<excludes>
<exclude>**/*.xml</exclude>
<exclude>**/*.properties</exclude>
<exclude>**/lib/**/*.js</exclude>
<exclude>**/lib/**/*.css</exclude>
</excludes>
<aggregations>
<aggregation>
<insertNewLine>true</insertNewLine>
<output>${project.build.directory}/${project.build.finalName}/app/myApp/js/all.min.js</output>
<includes>
<include>${project.basedir}/src/main/webapp/app/myApp/js/header.txt</include>
<include>**/*.min.js</include>
</includes>
</aggregation>
</aggregations>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.0.1</version>
<executions>
<execution>
<id>prepare-war</id>
<phase>prepare-package</phase>
<goals>
<goal>exploded</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.2</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<ignoreMissingFile>false</ignoreMissingFile>
<file>${project.build.directory}/myApp/index.jsp</file>
<replacements>
<replacement>
<token>PROJECT_VERSION</token>
<value>${project.version}</value>
</replacement>
</replacements>
</configuration>
</plugin>
I am looking for a way to copy all ressources to the target-directory, run yuicompressor, update the links and rename app/myApp to app/myApp-x.y.z
Compressing and updating the links already, but I can't find a way to rename my own app at build time.
Thanks
Solution
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<version>1.3.2</version>
<executions>
<execution>
<goals>
<goal>jslint</goal>
<goal>compress</goal>
</goals>
</execution>
</executions>
<configuration>
<excludeResources>true</excludeResources>
<removeIncluded>true</removeIncluded>
<suffix>.min</suffix>
<failOnWarning>true</failOnWarning>
<excludes>
<exclude>**/*.xml</exclude>
<exclude>**/*.properties</exclude>
<exclude>**/lib/**/*.js</exclude>
<exclude>**/lib/**/*.css</exclude>
</excludes>
<sourceDirectory>${project.basedir}/src/main/webapp/app/myApp</sourceDirectory>
<outputDirectory>${project.build.directory}/${project.build.finalName}/app/myApp-${project.version}</outputDirectory>
<aggregations>
<aggregation>
<insertNewLine>true</insertNewLine>
<output>${project.build.directory}/${project.build.finalName}/app/myApp-${project.version}/js/all.min.js</output>
<includes>
<include>${project.basedir}/src/main/webapp/app/myApp/js/header.txt</include>
<include>**/*.min.js</include>
</includes>
</aggregation>
</aggregations>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>prepare-war</id>
<phase>prepare-package</phase>
<goals>
<goal>exploded</goal>
</goals>
</execution>
</executions>
<configuration>
<useCache>true</useCache>
<!-- this exclude the directory to rename from the default copy resources procedure -->
<warSourceExcludes>app/myApp/**,.idea/**</warSourceExcludes>
<!-- this will do the renaming : i.e. we specify the source directory relative to pom.xml and the target directory relative to root -->
<webResources>
<resource>
<directory>src/main/webapp/app/myApp</directory>
<excludes>
<exclude>**/*.js</exclude>
</excludes>
<targetPath>/app/myApp-${project.version}</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.2</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<ignoreMissingFile>false</ignoreMissingFile>
<file>${project.build.directory}/myApp/index.jsp</file>
<replacements>
<replacement>
<token>%PROJECT_VERSION%</token>
<value>${project.version}</value>
</replacement>
</replacements>
</configuration>
</plugin>
</plugins>
I think you can do it by configuring a <webResources> element in your maven-war-plugin config.
Doing something like this should work
<configuration>
<!-- this exclude the directory to rename from the default copy resources procedure -->
<warSourceExcludes>app/myApp/**</warSourceExcludes>
<!-- this will do the renaming :
i.e. we specify the source directory relative to pom.xml
and the target directory relative to root -->
<webResources>
<resource>
<directory>src/main/webapp/app/myApp</directory>
<!-- override the destination directory for this resource -->
<targetPath>app/myApp-${project.version}</targetPath>
</resource>
</webResources>
</configuration>
EDIT
I'm not used to work with yucompressor plugin but it seems that you can easily change the output directory through <outputDirectory> element:
<outputDirectory>${project.build.directory}/${project.build.finalName}/app/myApp-${project.version}/js/all.min.js</outputDirectory>
If all your code is in all.min.js (I think that's what the yucompressor do, but I'm not sure) : then you don't need the <webResources> section in the war plugin, but you have to keep <warSourceExcludes>app/myApp/**</warSourceExcludes> to avoid duplication.
EDIT 2
As an answer to your comment.
Try the following:
use yucompressor for *.css and *.js as indicated in previous EDIT
exclude /myapp from usual war-plugin copy resources procedure using <warSourceExcludes>app/myApp/**</warSourceExcludes>
use a webResources section to include all you non *.js and non *.css to myApp-${project.version}:
<webResources>
<resource>
<directory>src/main/webapp/app/myApp</directory>
<excludes>
<exclude>**/*.js</exclude>
<exclude>**/*.css</exclude>
</excludes>
<!-- override the destination directory for this resource -->
<targetPath>app/myApp-${project.version}</targetPath>
</resource>
</webResources>

Resources