Directory outside of project base directory. Maven-war-plugin - <webResources> - java

I have Maven Problems warning in Eclipse, because of my pom.xml war-plugin configuration.
The project has multi modules. The hierarchy is here:
Project hierarchy
I need to pack content of specific directory (webapp in module2) to module. The main part of top pom.xml configuration is here:
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<webResources>
<resource>
<directory>../module2/src/main/webapp</directory>
</resource>
</webResources>
<packagingExcludes>**/*.db,**/*.scss,**/*.example,**/abrepository/**,,**/abconnection.xml</packagingExcludes>
<attachClasses>true</attachClasses>
</configuration>
</plugin>
</plugins>
The main part of module configuration is here:
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warName>modul</warName>
</configuration>
</plugin>
</plugins>
With this war-plugin config I have this warning which I want to exlude:
***module\..\module2\src\main\webapp" directory outside of project base directory. (org.apache.maven.plugins:maven-war-plugin:3.2.0:war:default-war:package) pom.xml /module line 1 Maven Build Participant Problem
Can I get rid of it, or can I add the content of the folder (webapp) to .war package in different way?
Thanks for any help.

Unlike the overwriting version warning, where on can add a comment "$NO-MVN-MAN-VER$" after the version, there does not seem to exist an elegant solution to this problem so far.
The only thing you can do (and I did in my case) is to disable "Maven Build Participant Problems" alltogether:
Problems View -> Arrow down button in the tor-right corner
"Configure Contents"
In your active config, deactivate within "Maven Problems" the check box "Maven Build Participant Problems"

Related

Maven changing output directory for jar doesn't work

I build my project with mvn clean install, however I want to generate a subfolder in the target folder and put the generated jar file in there. I saw these two questions Maven: specify the outputDirectory only for packaging a jar? and maven: how to place the output jar into another folder however their answer doesn't seem to work.
This is how my maven build looks like:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<outputDirectory>${project.build.directory}/blah</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
However I get the .jar file still in target directory. I also noticed that the project packaging is set as <packaging>eclipse-plugin</packaging> if I change this to jar, then it works fine, however I do need it to be eclipse-plugin. I am also using tycho for the eclipse plugin. Am I missing something that was not mentioned before?
From your packaging of eclipse-plugin I'm guessing you're using Tycho. Tycho doesn't seem to use any of the maven plugins, so configuring the maven-jar-plugin isn't going to help. Instead try configuring the tycho-packaging-plugin, specifically the buildDirectory property:
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-packaging-plugin</artifactId>
<version>${tycho-version}</version>
<configuration>
<buildDirectory>${project.build.directory}/blah</buildDirectory>
</configuration>
</plugin>

How to tell IntelliJ to use folder "web" instead of "webapp" for maven when building a war file?

What I do is:
Create a new project with IntelliJ with Maven module.
Add Framework support to this project and pick: JSF.
Go to pom.xml and add: packaging: war.
And from Maven window in IntelliJ I click: Clean Install.
Well build fails, because maven is looking for a webapp directory instead of a directory called web. For building the project.
So, if I rename the folder web to webapp build goes fine.
However, I want to learn more about IntelliJ and maven, so I want to force maven to use the folder web. How can I properly do this
Using the command line? I mean without invvolving IntelliJ at all?
Using Intellij?
Regards.
You can configure this in the pom.xml file for your project.
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warSourceDirectory>web</warSourceDirectory>
</configuration>
</plugin>
</plugins>
You can find the documentation here
If IntelliJ behaves as expected, it should pick up this new configuration.
Have a look at this post, which explains how to change the default webapp directory:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<warSourceDirectory>web</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>

Maven shade plugin adding dependency-reduced-pom.xml to base directory

The maven shade plugin is creating a file called dependency-reduced-pom.xml and also artifactname-shaded.jar and placing them in the base directory.
Is this a bug? Should be in the target directory. Any workaround?
You can avoid having it created by setting createDependencyReducedPom to false.
e.g.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>${maven-shade-plugin.version}</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
....
....
</plugin>
See more detail from apache
Based on bmargulies' answer and his comment on Xv.'s answer, I decided to configure the dependency-reduced POM to be output to target/, which is already ignored in my VCS.
To do that, I just added the dependencyReducedPomLocation element to the configuration element of the plugin, i.e.
<configuration>
<dependencyReducedPomLocation>${project.build.directory}/dependency-reduced-pom.xml</dependencyReducedPomLocation>
(...)
</configuration>
See https://issues.apache.org/jira/browse/MSHADE-121, and also https://issues.apache.org/jira/browse/MSHADE-124.
There is an option to move the d-r-p to elsewhere, but you may not like the consequences.
You are wrong about the -shaded jar, it always ends up in target/ unless you move it elsewhere.
You could use an old version of the plugin. Version 1.7 of the maven-shade-plugin writes to /target.
Since version 1.7.1, dependency-reduced pom.xml is written to basedir. See the issue MSHADE-124 for some reasons why it was done and what the consequences are. If you try setting dependencyReducedPomLocation, you will likely run into problems generating the site - open issue MSHADE-145.
the documentation on http://maven.apache.org/plugins/maven-shade-plugin/shade-mojo.html is incorrect when it says:
createDependencyReducedPom boolean - Flag whether to generate a
simplified POM for the shaded artifact. If set to true, dependencies
that have been included into the uber JAR will be removed from the
section of the generated POM. The reduced POM will be
named dependency-reduced-pom.xml and is stored into the same directory
as the shaded artifact. Unless you also specify
dependencyReducedPomLocation, the plugin will create a temporary file
named dependency-reduced-pom.xml in the project basedir. Default value
is: true.
the dependency-reduced-pom.xml is not stored in the same directory as the shaded artifact (target directory) ... it is in fact generated in the base directory, not target
To ignore the file you can add it to the ignore directive for your DVCS. For git, a .gitignore file is created with contents:
dependency-reduced-pom.xml
You can also add it to maven-clean-plugin configuration so it's blown away during the clean lifecycle phase: (Below assumes defaults, such as version, are defined in the POMs pluginManagement section.)
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<configuration>
<filesets>
<fileset>
<directory>.</directory>
<includes>
<include>**/dependency-reduced-pom.xml</include>
</includes>
<followSymlinks>false</followSymlinks>
</fileset>
</filesets>
</configuration>
</plugin>
</plugins>
</build>
Note that the above configuration is additive to the non-customized clean default.

Copy a file during maven build phase

My situation is:
I have a Maven project, I have my java classes in /app/src/main/java, my resources in /app/src/main/resources and my webapp files in /app/src/main/webapp
I have a javascript file in /common/script.js
Now what I want is to include (copy) the javascript file to the war file during the build phase of maven. To be precise, I want the script.js to land in /js/ directory of the war archive, just as it was placed in /app/src/main/webapp/js before starting the build.
I need this to share one version of resource files among many web-apps.
Kind regards,
Q.
You could do something like this, as documented here.
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<webResources>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>../common</directory>
<targetPath>/js</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
...
</project>
You can use the mojo copy-resources to copy resources which are not in the default maven layout or not declared in the build/resources element.
Check
"maven-resources-plugin"
You can use maven-resources plugin to copy a file to the desired location. Before or after a war has been built

Having Maven2 copy resources to the build directory, but NOT bundle them in the JAR

I've started a new Maven project in NetBeans, accepting all the defaults. The POM, with all the JAR dependencies stripped out, is cut-n-pasted at the bottom of this question.
The application reads in various properties files (e.g. logging and config). It also reads in external resources such as fonts, images, and sounds. I do NOT want all these resources to be bundled up into the JAR file. Instead, I plan to deploy them in subdirectories beneath the directory where the JAR is deployed.
A simplified view of the project's directory structure looks like this:
-src
|---main
|---java
|---com.mypackage, etc
|---resources
|---conf
|---fonts
|---images
|---sounds
+target
What I would like to have after a clean build would look like this:
+src
-target
|---myproject-1.0.0.jar (compiled contents of "src/main/java" ONLY)
|---conf
|---fonts
|---images
|---sounds
However, when I do a "clean-and-build" or an "exec" through NetBeans (or the command-line for that matter)... what I'm actually getting looks like this:
+src
-target
|---classes
|---("src/main/java" and "src/main/resources" slammed together)
|---myproject-1.0.0.jar (the "classes" subdirectory JAR'ed up)
Can someone point me in the right direction for getting that first result rather than the second? I apologize if this is a silly question (I'm a Maven rookie), or if I overlooked a previously-asked duplicate. However, from the searching I've done on Stack Overflow... it looks like all the duplicate questions try to go the other way! (i.e. get resources into a JAR rather than keep them out)
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>steveperkins</groupId>
<artifactId>myproject</artifactId>
<packaging>jar</packaging>
<version>1.0.0</version>
<name>My Project</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.4</source>
<target>1.4</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
...
Although the proposed solutions would work they basically work around the maven conventions. A better alternative would be to filter out the resources so they are not included in the jar but still available as resources while working in the IDE. In the pom it should look like this:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<excludes>
<exclude>/conf/**</exclude>
<exclude>/fonts/**</exclude>
<exclude>/images/**</exclude>
<exclude>/sounds/**</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
This would effectively exclude them from the jar without any workarounds.
Here is the doc page for the jar plugin.
Though the above will answer your question may I suggest some additional possibility that could help you in your endeavour. As a second step, to still make these resources available you could package your project using the assembly plugin. this would allow you to create a zip file and place all the files, resources and jar, in an appropriate location so that when the zip is unpacked everything just falls into place.
If this project is part of a larger work you can still use the assembly plugin for each where you would have this situation and in the main project you could extract and reassemble them in a larger zip including all the necessary artifacts.
Lastly I suggest you leave the directory structure under target as-is. If you customize it it would be preferable to do it through the Maven variables so that the changes percolate to the other plugins. If you manually remove and rename stuff once Maven has gone through you may run into problems later. Normally the Maven jar plugin should be able to just get it right if you configure it the way you want so you have no needs to worry about what comes under target. Personally I use Eclipse and the pusign is pretty good at getting the IDE and Maven config in sync. For NetBeans I would suspect this would also be the case. If not the best approach would be to configure your project in NetBeans to use target/classes as a target folder for built artifacts and target/test-classes for stuff built from src/test/java.
Personally, I would not use the default location of resources but an "extra" location and configure the resources plugin to copy them where you want from there:
<project>
...
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</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</outputDirectory>
<resources>
<resource>
<directory>src/non-packaged-resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
...
</build>
...
</project>
If you insist with using the default location (src/main/resources), I think you'll have to configure some exclusions (see below) to avoid resources getting copied by default and then use the same approach as above.
Another option would be to use the AntRun maven plugin and Ant to move files but this is not really the maven way so I won't detail it.
Resources
Copy Resources
Including and excluding files and directories
You can sonfigure a special execution of resources:copy-resources goal.
Eugene is on the right track but there's a better way to make this work.
It should look something like this:
<build>
<outputDirectory>target/${artifactId}-${version}</outputDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<classesDirectory>${project.build.outputDirectory}</classesDirectory>
<outputDirector>target</outputDirectory>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources/conf</directory>
<targetPath>../conf</targetPath>
</resource>
<resource>
<directory>src/main/resources/ANOTHER_PATH</directory>
<targetPath>../ANOTHER_PATH</targetPath>
</resource>
</resources>
</build>
You won't be able to get rid of the 'classes' directory, but you'll be able to give it a different name that shouldn't interfere with NetBeans.
You can find more about the <outputDirectory> element here.
You can find more about the jar plugin here.
You can find more about the <resource> element here.
As a side note, you may want to consider running Maven under a 1.6 JDK and fork the maven-compiler-plugin to use your 1.4 JDK for compiling. You can find out more about this here. That should give you a boost to your compile time. You can also tell surefire when running test cases to use the 1.4 JDK for execution as well.

Categories

Resources