Change a file xml with maven plugin - plugins

I´m working with maven 3, migrating the old application using hivemind and ant. I need to change The file "Hivemodule.xml" with some properties with information about enviroment, I define with profile but it doesn´t work.
I try using maven-resources-plugin but without success, perhaps it just do with properties files.
Detail: The Hivemodule.xml is inside the file jar and I want to unpack this file to turn the content of internal hivemodule configurations and after, to pack again... I´m in the unpack fase.
Here is my file in Hivemodule.xml and the parameters that I want to turn:
PROVIDER_URL="ormi://localhost:23791/"
APPLICATION_NAME="services_1_11"
SECURITY_PRINCIPAL="oc4jadmin"
SECURITY_CREDENTIAL="welcome"
For use of profiles, I edited this file:
PROVIDER_URL="${PROVIDER_URL}"
APPLICATION_NAME="${APPLICATION_NAME}"
SECURITY_PRINCIPAL="${SECURITY_PRINCIPAL}"
SECURITY_CREDENTIAL="${SECURITY_CREDENTIAL}"
In my pom.xml, the references to profile:
<profiles>
<profile>
<id>local</id>
<properties>
<PROVIDER_URL>ormi://localhost:23791/</PROVIDER_URL>
<APPLICATION_NAME>services_1_11</APPLICATION_NAME>
<SECURITY_PRINCIPAL>oc4jadmin</SECURITY_PRINCIPAL>
<SECURITY_CREDENTIAL>welcome</SECURITY_CREDENTIAL>
</properties>
</profile>
</profiles>
And my resource configurations:
<resources>
<resource>
<directory>${serviceSegPath}/${serviceName}/META-INF/</directory>
<filtering>true</filtering>
</resource>
</resources>
Is there any suggestion to solution this? Another plugin to make changes in xml file?

Perhaps this profile is not active when running maven.
mvn -P local clean install

Related

Using Maven's .m2/settings.xml throws warning in Eclipse m2e

I'm using Maven's settings.xml to override a property value for a log4j.properties file for development purposes. However, after I made this change, I now receive warnings in Eclipse m2e even though this is a normal use case:
Access "/Users/junger/.m2" directory outside of project base
directory.
(org.apache.maven.plugins:maven-resources-plugin:2.5:resources:default-resources:process-resources)
How do I remove this warning? Or, is there a bug tracking this? I couldn't find one.
In my pom.xml, I have -
<properties>
<log4j.properties.directory>src/main/java</log4j.properties.directory>
</properties>
In my settings.xml, I have -
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<log4j.properties.directory>/Users/junger/.m2/</log4j.properties.directory>
</properties>
</profile>
</profiles>
Looking at the code (the newScanner() method) of m2e, this seems to be an explicit warning when your project references files outside of its base directory.
For me, this warning is kind of justified. When you are referencing resources outside of your project's basedir, your builds might not be reproducible anymore. Your projects should rather be self contained without depending on external files.

displaytag.properties location in a maven web project

In a maven web project (developed with Eclipse on JBoss), I have put the file displaytag.properties
paging.banner.placement=both
in the following three locations:
/myapp/src/main/java/displaytag.properties
/myapp/src/main/resources/displaytag.properties
/myapp/src/displaytag.properties
However, the file is not recognized. It does not affect the produced table.
Any ideas how I could understand what is wrong?
From DisplayTag Configuration properties:
For the whole web application, create a custom properties file named
displaytag.properties and place it in the application classpath
(tipically into WEB-INF/classes). Displaytag will use the locale of
the request object to determine the locale of the property file to
use; if the key required does not exist in the specified file, the key
will be loaded from a more general property file.
There is no WEB-INF/classes in my folder structure, since this folder was not created by Maven.
UPDATE:
I've found out the reason why my properties files were not copied from /myapp/src/main/resources/*.properties to WEB-INF/classes in the generated war file. I've had the following setting in my pom file:
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>applicationContext.xml</include>
<include>security-config.xml</include>
</includes>
</resource>
<resource>
<targetPath>WEB-INF/classes</targetPath>
<filtering>false</filtering>
<directory>../src/main/resources</directory>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
</resource>
</resources>
WEB-INF/classes is the location where the classes of a webapp are found at runtime, inside the generated war file. The goal of Maven is to generate this war file. Check if the file is indeed in the war file generated by Maven.
The appropriate location for such a file in a Maven project is /myapp/src/main/resources/displaytag.properties. The files in this location are considered as resources which must be copied along with the classes in the generated artefact. If it's not the case, it means that the resources plugin isn't used, or is configured in a way that excludes this file or gets resources from another directory.

Missing package property files in war build

Littered throughout my project are various property files located in packages that are not being included when the package goal is run from a Maven build.
Using the 0.10.2 m2eclipse plugin and the default "package" goal.
In the project:
src->main->java->mypackage->MyProperties.java
src->main->java->mypackage->MyProperties.properties
In expanded war directory after the "package" goal is run:
target->classes->mypackage->MyProperties.class
-- no property file --
I'm trying to get the group to adopt Maven and resolving this issue is going to be a deal maker. Moving the properties files isn't going to be practical. Any help is much appreciated.
Put your property files where Application/Library resources belong, i.e. in src/main/resources:
src/main/resources/mypackage/MyProperties.properties
And it will get copied properly.
Pascal's answer is the correct maven way of doing things.
But some developers like to keep resources next to their sources (it's standard procedure in wicket, for example).
If you're going to do that, you will have to add your source folder to the resources:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
</build>

Maven assembly plugin: run only one descriptor

I have a project which has several custom descriptors written for the assembly plugin. Is there a way to run only one of those descriptors at a time instead of the whole bunch? I tried using the descriptors switch as documented here, passing in the full path to the one descriptor that I wanted to run, but instead it's running all of the descriptors in my app's main pom file, seeming to ignore the switch I specified.
Probably the easiest way to do so, is by using Maven Profiles.
Define some profiles in your pom.xml:
<profiles>
<profile>
<id>profile-1</id>
<properties>
<assembly-config>assem1.xml</assembly-config>
</properties>
</profile>
<profile>
<id>profile-2</id>
<properties>
<assembly-config>assem2.xml</assembly-config>
</properties>
</profile>
</profiles>
Then you use that particular property for the configuration of the assembly plugin:
...
<descriptor>src/main/assembly/${assembly-config}</descriptor>
...
Then run your maven build with the -P option: mvn -P profile-1 compile
So, summarized, if you choose a profile at buildtime, the property assembly-config will be set depending on the defined profile. The assembly configuration depends in that case on the chosen profile.
Hope this helps!

Porting a tomcat web project from eclipse ganymede to intellij 8.1

I have a standard (I think) web project developed with the eclipse IDE. I wish to port it to Intellij idea 8.1 - I think that, among other things, it has better taglib support.
My project structure is as follows:
Project Folder
./src [java source files etc.]
./conf [configuration files - log4j, spring beans...]
./buid [ant files]
./WebContent
./WebContent/images [image files]
./WebContent/META-INF
./WebContent/META-INF/context.xml
./WebContent/pages [.jsp+.html files]
./WebContent/scripts [.js files]
./WebContent/skins [.css files]
./WebContent/WEB-INF
./WebContent/WEB-INF/classes [.class files]
./WebContent/WEB-INF/lib [.jar files]
./WebContent/WEB-INF/tags [.tag files]
./WebContent/WEB-INF/web.xml
I can't seem to get this project configured with my local tomcat server (version: apache-tomcat-6.0.18).
I think that a good answer would be a standard, step by step, cookbook answer as to how to port (and perhaps how to correctly define a tomcat web application within intellij idea).
Thanks all!
I think the first step would be to create a stand-alone build file which will produce a WAR. Do this before attempting to import the project into InteliJ.
I would use Maven. Creating a maven POM file to create a WAR is almost trivial and you can easily override the the default locations for your src, conf, and web content to match you existing src directory. Then test the build by deploying your newly Maven created WAR to Tomcat. I wouldn't think this first task would take more than a half day (at most a full day).
IntelliJ has a built in utility to import Maven projects. Then you should be off and running....
Regardless of the IDE you finally settle on, your project will be much better off in the long run for the Maven migration.
You initial Maven POM file will look something like this...
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yourcompany.yourapp</groupId>
<artifactId>yourapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>Your project name here</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
*** other dependencies here ***
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>conf</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.0</version>
<configuration>
<webResources>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>WebContent</directory>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
</project>
*** This is an example POM only... It's just meant to get you started and may not work "as is".
Start off by creating an empty web application for tomcat, within IntelliJ - and make sure that it deploys correctly
This will produce a directory structure that you should then be able to copy your source files/web assets into.
The thing that you'll probably need to handle differently is the lib files - don't store these directly in the WEB-INF directory, as keeping them in a separate 'library' area, and allowing the IDE to include them in the WAR at build time is generally a better approach, as it promotes re-use across projects.
The key thing to aim for is to not try to set your project up to completely mirror a tomcat application, as the build process will pull together the various parts for you. It all breaks down into 3 sections...
Static assets - images, config files and jsp files (Ok, I know JSP files are kinda dynamic)
Java classes - source code that you write yourself (The IDE will compile these and place them in the appropriate location)
Java Libraries - third party code that you compile against (Again the IDE will place these in the appropriate location)
There are a few bits of configuration, within the project file, that you'll need to tweak to suit your needs, but it's generally straightforward.
By default, log4j will look for it's configuration file (either log4j.xml or log4j.properties) from the classpath of your application. So this means you should place it in WEB-INF\classes, or you can specify a different location with the environment variable log4j.configuration. See the log4j manual.
What IDE you use should have no impact on the structure of your application when it gets deployed to your servlet container. It sounds like maybe you were relying on Eclipse to package the files in a specific way - this is probably a bad practice. Are you using an actual build script?