Can jboss-web.xml have access to properties? - maven

I am trying to setup my project to use different virtual-host names depending on the environment.
I know I could create directories with a separate jboss-web.xml file in each directory. But I recently moved this project to maven and wanted to take advantage of the profiles. I already have the databases set up so they can be configured differently by the profile by using filters.
jboss-web.xml:
<jboss-web>
<context-root>/</context-root>
<virtual-host>${jbossweb.virtualhost}</virtual-host>
</jboss-web>
The environment specific property file has an entry for:
jbossweb.virtualhost=hostname.domain.com
the pom file build section has this definition
<filters>
<filter>src/main/filters/filter-${env}.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/webapp/WEB-INF</directory>
<filtering>true</filtering>
</resource>
</resources>
Just for good measure, the profiles section has this configuration:
<profile>
<id>dev</id>
<properties>
<env>dev</env>
</properties>
</profile>
I'm not even sure if what I'm trying to do is possible. When I try it, Jboss-web.xml has the variable name in the virtual-host location and not the value.
Am I barking up the wrong tree?
Thanks,
Eric

Actually, I've discovered a way to do this:
The jboss-web.xml looks like this now:
<jboss-web>
<context-root>${jbossweb.contextroot}</context-root>
<virtual-host>${jbossweb.virtualhost}</virtual-host>
</jboss-web>
The property file looks like this:
jbossweb.virtualhost=hostname
jbossweb.contextroot=/
Then the pom section looks like this for the maven war plugin configuration:
<webResources>
<resource>
<directory>src/main/webapp/WEB-INF</directory>
<filtering>true</filtering>
<include>jboss-web.xml</include>
<targetPath>/WEB-INF</targetPath>
</resource>
</webResources>
And the filters are defined above the plug ins section, but within the build section as follows:
<filters>
<filter>src/main/filters/filter-${env}.properties</filter>
</filters>

Related

Maven Resources and docker-maven-plugin

I would like to understand better how Maven deals with resources and in particular how plugins can reuse the Resources infrastructure that Maven provides:
I have already read the resources section of Maven and looked at the Maven Resources Plugin.
Now I came across the Docker Maven Plugin from Spotify. Which contains the following configuration section:
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
Looking at the Readme of the plugin I could not find any explanation which child elements are allowed for a <resource> element. I am assuming that the <resource> element is in fact related to Maven's org.apache.maven.model.Resource class and that I can just reuse the documentation of Maven to understand who it works. But according to the Maven documentation I linked above, it seems that <include> elements must be nested inside an <includes> element which the above code listing does not do. Now I am confused.
To summarize: Where can I look to get a definitive answer if a plugin does not exactly document how its configuration works. Does there exist some kind of XML schema reference for plugins where I can lookup how particular elements work?
I think you want to set configs like:
<dockerDirectory>src/main/docker</dockerDirectory>
<dockerHost>https://192.168.99.100:2376</dockerHost>
<dockerCertPath>/Users/your_user/.docker/machine/machines/default</dockerCertPath>
these one you can use for example by following:
fixes this by:
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<configuration>
<imageName>yourImageName</imageName>
<dockerDirectory>src/main/docker</dockerDirectory>
<dockerHost>https://192.168.99.100:2376</dockerHost>
<dockerCertPath>/Users/your_user/.docker/machine/machines/default</dockerCertPath>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
Important are these two tags:
<dockerHost>https://192.168.99.100:2376</dockerHost>
<dockerCertPath>/Users/your_user/.docker/machine/machines/default</dockerCertPath>
I am using a dockerfile, which path you have to define with this tag:
<dockerDirectory>src/main/docker</dockerDirectory>
Now you can build your jar and generate docker image via:
mvn package docker:build

several resource rules are not properly recognized by maven eclipse:eclipse

I have a pom.xml with the following under <build>:
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<!-- exclude binary files. They get corrupted by filtering -->
<exclude>**/*.zip</exclude>
</excludes>
</resource>
<!-- copy binary files separately without filtering. They get corrupted by filtering -->
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>**/*.zip</include>
</includes>
</resource>
</resources>
Now this works perfectly when I build a jar using maven install. It then excludes the zip files from filtering, but copies them separately whithout filtering.
However, when I do a maven eclipse:eclipse, I see in eclipse under (right click) MyProject->Properties->Java Build Path->Source->MyProject/src/main/resources that **/*.zip is excluded. So apparently only the first <resource> element in my pom is recognized.
I need both recognized, since I also need the zip file copied to targetEclipse when working in dev environment.
Any idea how to accomplish this?
The maven docu about eclipse:eclipse states:
Invokes the execution of the lifecycle phase generate-resources prior to executing itself.
So regarding the resources it should just do the same as when I execute maven install.
So I figured it out myself...:
Apparently the eclipse:eclipse only allows one configuration per resource directory and ignores all others.
So I created a second resource directoryx, where I put all the binary files. I now only apply the filtering to the one containing no binary files. That way it works both with eclipse:eclipse and install:
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
</resource>
<!-- copy binary files separately without filtering. They get corrupted by filtering -->
<resource>
<directory>${basedir}/src/main/bin</directory>
<filtering>false</filtering>
</resource>
</resources>

Maven Resource Filtering Breaking Downloads

I have a Maven/Java application. Part of the application allows you to download some documents. Here is the project setup.
+src
+main
+resources
+downloads
MyDocument.docx
jdbc.properties
pom.xml
The downloads work correctly when jdbc.properties has the hardcoded values inside them. However, I am trying to update the application to use Maven profiles and specify the different database connections for the different environments. I manage to get it to work with the following in pom.xml.
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
However, even though the jdbc.properties file gets correctly populated with the correct environment database information, the download feature stopped working. The file will be downloaded, but when you try to open it, it says The file MyDocument.docx cannot be opened because there are problems with the contents..
I tried changing the <directory> to src/main/resources/*.properties and adding an additional <resource> where I turn the <filtering> to false for src/main/resources/downloads. But neither approach worked. How can I prevent the Maven filtering from corrupting the files?
FYI - I looked inside the WAR and the documents cannot be opened from there either (they are already corrupt).
Update: Better solution from https://stackoverflow.com/a/10025058/516167
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<configuration>
<encoding>UTF-8</encoding>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>xls</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
You should exclude files like MyDocument.docx (*.docs) from filtering.
<build>
<resources>
<resource>
<directory>src/main/resouces</directory>
<filtering>true</filtering>
<excludes>
<exclude>**/*.docx</exclude>
</excludes>
</resource>
</resources>
</build>
or define alternate directory for downloads like:
<build>
<resources>
<resource>
<directory>src/main/downloads</directory>
</resource>
</resources>
</build>

Maven Properties not referenced in properties file

I have some properties in my maven pom.xml.
<properties>
<number>3</number>
<age>38</age>
</properties>
(They are random properties)
In a properties file, lets call it resource.properties, I have the following:
value1 = ${number}
value2 = ${age}
When spring tries to read properties from this file, it cannot get the reference from ${number} saying that it cannot be found.
Why is this and how can I make it work? Or is doing this not possible at all.
EDIT: I have enabled filtering but still does not work. My resource is in the src/test/resources directory.
Here is the part of the pom where I enable filtering.
<build>
...
<resources>
<resource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
...
</build>
you need to tell maven which files it uses to replace placeholders e.g
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
see maven filter
You should enable resource filtering for the maven resources plugin as shown below:
...
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
...
See the Maven Resources Plugin section about filtering for details.

Maven - Exclude certain resource files into WAR from the default src/main/resources location

Currently, I want to exclude some files from the default src/main/resources folder into my WAR when packaging
I tried using maven-war-plugin with the following configuration but failed.
<webResources>
<resource>
<directory>src/main/resources</directory>
<targetPath>WEB-INF/classes</targetPath>
<excludes>
<exclude>*.xml</exclude>
</excludes>
</resource>
</webResources>
...WEB-INF/classes will still contain the XML files.
How to do so?
As pointed out in https://stackoverflow.com/a/2737635/722997, a quick way to exclude files from the WAR package is to exclude them in the build->resources section, like:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>*.xml</exclude>
</excludes>
</resource>
</resources>
...
</build>
Note: take into account that the following configuration will only affect default executions of Maven (WAR package, JAR package, ...), but not assemblies or other user configurations.
This is somewhat late to this question, but I was just trying to do the same thing, and have found that (with the maven-war-plugin 3.1.0 version), adding:
<packagingExcludes>WEB-INF/classes/*.xml</packagingExcludes>
to the configuration should do what was asked for (it worked for me to remove properties files we didn't want to distribute with the war file).
From the documentation of maven war plugin, you can include and exclude resources as follows:
...
<configuration>
<webResources>
<resource>
<!-- the default value is ** -->
<includes>
<include>**/pattern1</include>
<include>*pattern2</include>
<includes>
<!-- there's no default value for this -->
<excludes>
<exclude>*pattern3/pattern3</exclude>
<exclude>pattern4/pattern4</exclude>
</excludes>
</resource>
</webResources>
</configuration>
...
Are you following this and it still does not work? If so, can you post your pom snippet?

Resources