How does IntelliJ's Maven filtering support work? - maven

I have noticed when you configure a Maven project to use property filtering the property filtering seems to also work during a non-maven IntelliJ "make". This means the IntelliJ run configurations for Jetty/Tomcat/GWT/Glassfish will still honour your maven resource filtering.
So if I add this to my pom.xml:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
<include>**/persistence.xml</include>
</includes>
</resource> ....
It should filter any properties in my properties and peristence.xml files before any intellij run configurations start. This is very usefull for swapping in JDBC references or filesystem parameters.
The only problem I am having is that IntelliJ only seems to honour filtering in src/main/resources even if I change pom.xml to have a second entry for other directories (ie:src/integrationtest/resources).
This all seems to be "automagical". So how does it work and where (if anywhere) can I configure it?

IntelliJ IDEA's Make features are capable of filtering Maven resources. However, IntelliJ IDEA yet does not support filtering web resources.
source: http://www.jetbrains.com/idea/webhelp/maven.html#compile
No further details about this support in whole intellij webhelp though, so I guess it should work just like maven's process-resources phase does.
The problems you are having can be caused by the fact that directory src/integrationtest/resources doesn't follow maven conventions.
Maybe it will work if you:
make it src/test/resources/integrationtest/
or
configure maven to respect src/integrationtest as test sources (but if integrationtest isn't well-known convention it will be violation of maven's COC rule)
or
make it another maven (sub)module, if you want to emphasize isolation of integrationtest
As for filtering directories different that src/main/resources: filtering src/main/webapp/META-INF worked out-of-a-box for me.
(Maven 3.0.4, Intellij 12.1.4)

Good news, looks like the issue will be fixed in 13.1
http://youtrack.jetbrains.com/issue/IDEA-25934
EDIT: Sorry if not clear enough, the bug case is just marked as "fixed" with no further explanation...
But I tested in 13.1 EAP version (build 134.1445) and while previously IntelliJ would overwrite the resources, it now preserves the web resources filtered by Maven.

Intellij (I'm using 14.1) does allow you to define custom Ant tasks as pre-/post-processing during artifact build.
Go to Project Structure -> Artifacts -> {select artifact} -> {Pre-processing|Post-processing} tabs.
So, for example, I can use the following simple task to simulate resource filtering in cases where it doesn't work out of the box:
<target name="filter" depends="clean">
<copy todir="${maven.build.dir}/${maven.build.finalName}">
<fileset dir="${maven.build.resourceDir.0}"/>
<filterset begintoken="${" endtoken="}">
<filter token="project.version" value="${project.version}"/>
</filterset>
</copy>
</target>

Don't forget to define a default profile
<profiles>
<profile>
<id>development</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
...
</properties>
</profile>
</profiles>

Related

Is It Possible to Set the user.agent Property from the Command Line Using the GWT-Maven-Plugin?

I know that in my *.gwt.xml file I can specify the browsers I want the GWT compiler to compile my app for by adding this to it:
<set-property name="user.agent" value="opera,ie8, gecko1_8, safari, ie9"/>
Is it possible for me to set this property on the command line when I build my project through maven? I'd like to be able to do something like this when I'm developing locally on my machine:
mvn clean install -Duser.agent="opera,ie8"
EDIT: Starting with GWT 2.7, you can now pass a -setProperty user.agent=… on the command line; no need to tweak gwt.xml files any more. I'm not sure Mojo Plugin for GWT let you use that though, but the net.ltgt.gwt.maven Maven Plugin for GWT can.
You can use filtering of your resources, but then it might make it harder to work from within your IDE.
In your gwt.xml:
<set-property name="user.agent" value="${user.agent}" />
Then in your pom.xml:
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
and the default value for the property when you don't give it on the command-line:
<properties>
<user.agent>opera,ie8,gecko1_8,safari,ie9</user.agent>
</properties>
Note however that this goes against The Maven Way™.

read Maven variable from properties file using profile

I want to read a maven variable for configure a build plugin from a properties file. It's not needed in and further project files e.g. context files.
1) made a profile (it works, can use mvn ... -P private)
<profile>
<id>private</id>
<properties>
<env>private</env>
</properties>
</profile>
2) created the filter file with this content (it works)
foo.path=/home/foo/path
3) try to configure the plugin (does not work)
<build>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>foo-plugin</artifactId>
<version>${foo-plugin.version}</version>
<configuration>
<!--<fooPath>home/foo/path></fooPath> that works -->
<fooPath>${foo.path}</fooPath> <!--works not -->
</configuration>
...
</build>
Thx a lot
The name of your property is 'env' but you don't use env anywhere in your configuration.
When Maven docs mention "filter files" they usually mean a file used when processing resources (i.e. copying resources from /src/main/resources to target/classes). As far as I know the properties in those files aren't used for plugin configuration out-of-the-box. I have used the Codehaus properties-maven-plugin:read-project-properties goal do do what you are attempting. Make sure you bind the goal to the lifecycle before any plugins that need the properties for config.
Also, see this answer; you may load properties used to configure other plugins, but not to configure core Maven project elements.

I can't get maven to use properties defined in ~/.m2/settings.xml?

I am using ~/.m2/settings.xml to store a number of property names used throughout the pom.xml files in my project. If I make the XML invalid (by adding another < for example), maven immediately generates an error, saying that it cannot parse that file. If I leave the XML valid, settings in my appBeans.xml file do not pick of references to properties defined in settings.xml.
Has anyone experienced this problem? I am sort of at my wits end here.
Reflecting properties from Maven configurations works by resources filtering.
Make sure your settings.xml, project pom and the target xml file contain correct configurations and reside in correct places.
If I understood correctly, you want to store a property name and value in the settings.xml so the props can be used in your project files. I'll provide a working example:
Define a default profile and properties in settings.xml:
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<someProp>Value</someProp>
</properties>
</profile>
</profiles>
Define resource folder's filtering=true in pom.xml:
<build>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
Define the property in the xml file (that resides in the src/main/resources folder):
...
<element>${someProp}</element>
...
After this you should see the filtered result e.g. in target/classes/appBeans.xml.
Bear in mind that if you're using Eclipse & m2eclipse or similar plugin, it probably won't start using the updated settings.xml without restarting Eclipse and it's automatic build will sometimes overwrite your files in the target folder. I'm talking from experience here :)
Maven properties do not get reflected in miscellaneous XML files.
If you add one of these properties to the <properties/> element of the specific pom that runs the specific plugin that reads allBeans.xml, does that work? I believe that it will not, and your problem will turn out to be adding to the <configuration/> for the plugin to pass the maven properties to it.
If you edit your question to show the plugin that processes appBeans.xml I can make this more specific.

How to filter resources when using maven jetty plugin?

I have an XML file (urlrewrite.xml) that needs a property placeholder resolved. I enable Maven filtering to achieve this. This works fine for the assembled WAR file.
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
The problem is when trying to run the application in development mode using the maven-jetty-plugin (Maven Jetty Plugin), as maven jetty:run .
The file in question, urlrewrite.xml, is located in the src/main/resources directory, and therefore should (and does) ends up in /WEB-INF/classes (or target/classes for maven jetty:run).
The URLRewriteFilter config specifies the location of the config file as follows:
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
<init-param>
<param-name>confPath</param-name>
<param-value>/WEB-INF/classes/urlrewrite.xml</param-value>
</init-param>
</filter>
This will work at deployment time. However, Using the jetty maven plugin, URLRewrite will die with a NullPointerException because it uses context.getResourceAsString("/WEB-INF/classes/urlrewrite.xml") in order to load the config file. Jetty returns null for this because when running the application from workspace it resolves /WEB-INF/classes/... to src/main/webapp/WEB-INF/... . The file does not exist there because the WAR has not yet been assembled. It should instead pull the resource from target/classes/urlrewrite.xml.
If that is obscure to you, then you probably won't be able to answer this question because I suspect you will need to be a Jetty guru to figure out a workaround (hint: that's a challenge!).
Does anyone know a way around this? I have also tried the following workarounds to know avail:
Put urlrewrite.xml under a new directory, src/main/webResources and add it to the maven war plugin <webReources> and enable filtering. That will copy it's contents in the appropriate location when the WAR is packaged, but will not make it available for jetty:run
Some other hacks I can't even remember ... (will update if I do)
In summary, maven-jetty-plugin needs the file to be under src/main/resources/webapp/insert path and filename in order to be available for the maven jetty:run command ...
Thanks for you help ...
Sincerely,
Lloyd Force
Answered my own question.
Upgrade maven-jetty-plugin to at least 6.1.12
See this wiki page on 'Configuring Multiple WebApp Source Directory' (available since jetty-6.1.12.rc2 and jetty-7.0.0pre3)
Add some magic to pom.xml:
First, add a new directory (src/main/webResources) for your filtered web resources and add a <resource> element:
<resource>
<directory>src/main/webResources</directory>
<filtering>true</filtering>
<targetPath>../jettyFilteredResources</targetPath>
</resource>
That will copy the files to target/jettyFilteredResources (we will reference this later). This directory will NOT get copied to your packaged WAR file, it is for jetty only!
Add the following element to your maven-war-plugin <configuration> element:
<webResources>
<resource>
<directory>src/main/webResources</directory>
<filtering>true</filtering>
</resource>
</webResources>
That will ensure everything is packaged up for your real WAR file.
Finally, tell jetty to use the resources your copied especially for it, by added the following snippet to your <baseResource> element:
<baseResource implementation="org.mortbay.resource.ResourceCollection">
<resourcesAsCSV>src/main/webapp,target/jettyFilteredResources</resourcesAsCSV>
</baseResource>
Now everything will worketh! (Well, technically I haven't tested the production WAR yet, but ... blah ... it should work too).
If anyone has a better answer, I will accept it provided the answer is provided in a reasonable amount of time (say 1 day).
I think the answer in this other question is better:
Running resource filters when using jetty:run
Basically, instead of running 'mvn jetty:run' you have to use 'mvn jetty:run-exploded'.
The only drawback is that it needs to build the WAR file, which might be expensive in some cases. If that's not an issue for you, then I guess it's better.
add this to pom.xml:
<resources>
<resource>
<directory>src/main/webapp/WEB-INF</directory>
<filtering>true</filtering>
<targetPath>../jettyFilteredResources</targetPath>
</resource>
</resources>
and this is how embedded Jetty server should look like:
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.1.3.v20140225</version>
<configuration>
<webAppConfig>
<descriptor>target/jettyFilteredResources/web.xml</descriptor>
</webAppConfig>
<scanIntervalSeconds>3</scanIntervalSeconds>
</configuration>
</plugin>
woila! thanks #les2 for inspiration ;-)
I found another way.
build the project and add the target folder as extra classpath.
<webAppConfig>
....
<extraClasspath>${basedir}/target/mywebapp</extraClasspath>
....
</webAppConfig>

Is there a way to remotely deploy EAR to Oracle AS using maven?

Is there a way to remotely deploy an EAR after building it to Oracle AS? Remote deployment functionality exists for Eclipse, and I'm looking for something similar for maven2:
http://download.oracle.com/docs/cd/E14545_01/help/oracle.eclipse.tools.weblogic.doc/html/conFeatureOverview.html#remoteDep
Verified on Oc4j 10
http://chadthedeveloper.blogspot.com/2008/05/automated-deployment-to-oc4j-in-maven2.html
Have you tried the Weblogic Maven plugin? The weblogic:deploy goal seems to do exactly what you want. I've not used it myself so can't confirm if it actually works or not.
Update:
Found this blog that describes deploying to 10.1.3, though not using the weblogic plugin.
This is not a complete answer your question; is's just a checklist for things to look up while solving this:
what to do with generated sources, when to generate them?
do you need custom information in META-INF?
how to manage jars?
you can have them installed in an external repository, local repository, or specify them with system, on a project relative path, or a system absolute path
if specified with system, you can keep compile time jars in any location, and those that you want inside wars inside webapp/WEB-INF
If your project has a maven friendly architecture, then ok. Otherwise you can specify custom paths like this:
<packaging>ear</packaging>
<build>
<finalName>ear-name</finalName>
<!-- you can have only one source path-->
<sourceDirectory>src-dir-path</sourceDirectory>
<!-- you can have only one test path-->
<testSourceDirectory>test-dir-path</testSourceDirectory>
<!-- you can have several resource paths -->
<resources>
<resource>
<directory>src-resources-path</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<!-- you can have several test resource paths -->
<testResources>
<testResource>
<directory>test-resources-path</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</testResource>
</testResources>
</build>
Other tips:
study Eclipse generated ear, and maven generated ear, check for differences
you can find missing jars with mvn compile, don't use an IDE for this, you want to make sure that maven has all the jars it needs
you can have ant scripts inside maven, use maven-antrun-plugin, i can provide examples if requested

Resources