redefine webapp location - maven standard folder for web resources - java

I have big Java project build with Ant, that I am converting to maven.
How to redefine webapp - maven standard folder for web resources?
I can't move web content, and it is always under active development.

See here:
http://maven.apache.org/plugins/maven-war-plugin/war-mojo.html
The property you want to change is "warSourceDirectory"

Solved by
<!-- http://maven.apache.org/plugins/maven-war-plugin/examples/adding-filtering-webresources.html -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<webResources>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>${webapp-folder}</directory>
<excludes>
<exclude>**/*.jar</exclude>
</excludes>
</resource>
</webResources>
</configuration>
</plugin>

Related

Eclipse (latest) artifact builder does not include ressources

i'm currently confused that eclipse ignores my configured pom building instructions.
Used : Eclipse 2023-03 / 2022-12 (both same failure)
parent pom :
...
<modules>
<module>my-module</module>
<module>my-webapp</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>my-module</artifactId>
<version>1.0</version>
</dependency>
...
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.12.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<excludes>
<exclude>**/module.dtd</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.14.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>17</source>
<target>17</target>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
my-module structure (pom refers to parent without any build instructions - packaging -> jar) :
src/main/java
---...
src/main/resources
src/main/resources/META-INF/test/file.xml
src/main/resources/example/bootstrap/file2.xml
my-webapp (refers to parent pom - packaging -> war) :
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resource>
<directory>${config.path}</directory>
<targetPath>WEB-INF/config</targetPath>
<includes>
<include>**/*</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
Build with mvn clean install results in a deployable war file which contains my-module.jar in lib folder as expected, size matches also the generated jar file in my-module targets folder.
so targets generated .war/.jar are correct.
Now i setup inside eclipse a default tomcat server above v.9.0.50+ (tried multiple versions, also latest 9.0.71). and added my-webapp(example-context) to the server (cleaned before and then publish).
at this point i inspected my unzipped war file inside tomcat webapproot (tried meta and tomcat location (both same failure)) , and was completly confused that my my-module-1.0.jar file only contains only META-INF/maven (pom.xml + pom.properties) and my java classes so that it doesen't match the size as expected (due to missing resource files).
Is there any trick or option to configure eclipse to build the artifact with resources as instructed by my poms or any other vaiable soloution ?
If i use intellij, it worked out of the box but i prefer eclipse for my project
Tried different versions of eclipse , tomcat , maven and plugin versions (latest) but nothing seems to work.
Sometimes if i change my resource or java files of my-mopdule, eclipse makes a redeploy and then the artifact contains the correct my-module-1.0.jar with resources included but after another mvn clean install its gone and i need to modify any files again up to 10 times if it happens again.

Why is maven-war-plugin ignoring my configured filters for web resources?

I have a filter file in src/main/filters/base.properties with contents:
testProperty=testValue
My POM has resources, filtering, and additional web resources defined (using maven-war-plugin). It also overrides the default filtering delimiter.
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<filters>
<filter>src/main/filters/base.properties</filter>
</filters>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<overwrite>true</overwrite>
<useDefaultDelimiters>false</useDefaultDelimiters>
<delimiters>
<delimiter>#</delimiter>
</delimiters>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resource>
<directory>src/main/more-web-resources</directory>
<filtering>true</filtering>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
All filtering works fine for files located in src/main/resources. However, a configuration file located in src/main/more-web-resources is not being filtered properly. So if I have a file src/main/more-web-resources/test.properties, with contents of:
finalTestProperty=#testProperty#
The final src/main/webapp/test.properties file looks like:
finalTestProperty=#testProperty#
As opposed to:
finalTestProperty=testValue
So, filtering just isn't working for the additional web resources specified with the maven-war-plugin. What am I missing?
Well, after a night of sleep, I came back to it this morning, built, and the properties are being filtered properly. I made sure my POM and project structure matches my question's exactly, and it does. It's possible I was looking at the original test.properties and not the built test.properties.
To be clear, and to address the comments in the question, I did not have to define an additional resource in the <resources/> section for the more-web-resources directory, I did not have to (re-)specify the filters in another <filters/> section inside the maven-war-plugin configuration, and I did not have to (re-)specify the # delimiter inside the maven-war-plugin. It all just works as I originally expected it to, by using the resources and filters defined in the standard sections of the POM, as well as the maven-resources-plugin.

Maven Exclude Files During Build

I am having problems getting Maven to build my webapp without including extraneous development file, such as unminified script and css files.
First i tried using exclude in combination with webResources
<build>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warName>ReportRocket-1</warName>
<webResources>
<resource>
<directory>src/main/webapp/resources/</directory>
<excludes>
<exclude>annotated-js/*.js</exclude>
<exclude>compiled-js/*.js</exclude>
<exclude>css/*.css</exclude>
<exclude>less/*.less/exclude>
</excludes>
<directory>src/main/webapp/WEB-INF</directory>
<excludes>
<exclude>connection.json</exclude>
<exclude>reportRocket.jsp</exclude>
</excludes>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
The result was the contents of WEB-INF being duplicated in the project root and no excluded directories or files.
So, I looked around here and found this: maven2: excluding directory from WAR but running mvn clean package using either warSourceExcludes or packagingExcludes results in the directories i'm trying to exclude not being, well, excluded...
The build section of my pom.xml
<build>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warName>ReportRocket-1</warName>
<packagingExcludes>
src/main/webapp/resources/annotated-js/,
src/main/webapp/resources/compiled-js/,
src/main/webapp/resources/css/,
src/main/webapp/resources/less/,
src/main/webapp/WEB-INF/connection.json
</packagingExcludes>
</configuration>
</plugin>
</plugins>
</build>
Building the project with these settings results in the following project structure:
META-INF
resources
//desired folders
annotated-js
compiled-js
css
less
WEB-INF
// desired files
connection.json
This is my first time using a build tool, so i'm probably overlooking something simple but in the meantime, this is driving me crazy. Any suggestions or obvious problems with my xml?
First you should read the documentation of the maven-war-plugin cause the packagingExclude is intended for a different purpose, but in your case you need to do the configuration in that way:
<configuration>
<webResources>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>src/main/webapp/resources/</directory>
<!-- there's no default value for this -->
<excludes>
<exclude>annotated-js/**</exclude>
</excludes>
</resource>
</webResources>
</configuration>

maven-jar-plugin file filterring

I have a some perl file in my src/main/java/com/pac/report.pl which I want to package as part of my classes in the jar file.
Using maven maven-jar-plugin include directives, I have tried below and various other suggestions I pulled off the web, but doesn't copy the perl file as part of my classes in the jar file. Does anyone know what I am doing wrong.
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<includes>
<include>**/*</include>
</includes>
</configuration>
</plugin>
EDIT
Also let me point out that I don't want to place the file in the resource directory due to legacy call and dependent reasons.
That is because the classes packaged into your jar aren't taken from src, but rather from target (specifically /target/classes), and the compiler completely ignores your non-java file.
Try placing your file in src/main/resources/com/pac/report.pl and it should be packaged into the jar (with the relative path of /com/pac/report.pl) since thats the default location where the resources plugin looks for additional files to add to /target before the jar plugin runs.
EDIT - or, if you dont want to / cant do this the way maven expects, you could manually bind an execution of the resources plugin to the lifecycle to pick up your file and copy it over to target. something like this:
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>compile</phase> <!-- or later -->
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/classes</outputDirectory>
<resources>
<resource>
<!-- path to your *.pl file here -->
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
...
</build>

How to exclude classes from a packaged webapp with maven

How can I filter certain classes in /target/classes from going into /target/[webapp]/WEB-INF/classes? I want them compiled into /target/classes/ but not in the final war.
What are these classes for? If they are for testing, you can specify them in src/test/java, they will then be compiled into target/test-classes in the test-compile phase, but won't be included in the final war.
If they aren't for testing and aren't to be included in the war, perhaps they should be refactored into another project so you can specify it as a dependency (perhaps with "provided" scope so they won't be deployed.
For reference you can configure the war to include and exclude resources when packaging.
The following example will include all jpgs but exclude resources from the image2 sub folder:
<configuration>
<webResources>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>resource2</directory>
<!-- the list has a default value of ** -->
<includes>
<include>**/*.jpg</include>
<includes>
<excludes>
<exclude>**/image2</exclude>
</excludes>
</resource>
</webResources>
</configuration>
See the war plugin documentation for more details.
You can use the TrueZIP Maven Plugin ( http://mojo.codehaus.org/truezip-maven-plugin/ ).
See examples in:
http://svn.codehaus.org/mojo/trunk/mojo/truezip-maven-plugin/src/it/
You might have luck with this, assuming you them in a package that you can define with an ant pattern
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<excludes>**/dontneed/*.class</excludes>
</configuration>
</plugin>
With current version of maven-war-plugin (3.0.0) this works for me -
<profile>
<id>abc</id>
...
<build>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<packagingExcludes>WEB-INF/classes/com/abc/pqr/ClassName.class</packagingExcludes>
</configuration>
</plugin>
</build>
</profile>

Categories