maven bundle plugin No translation found for macro: - spring

I use maven bundle plugin for bundle spring project. I'm use spring property placeholder in my project. When I building my project I take following warnings:
[WARNING] Bundle groupId:artifactId:bundle:1.9-SNAPSHOT : No translation found for macro: spring.property
How I can prevent this warnings message ? May be some maven bundle plugin settings can help me ?
Thanks

It appears that the maven-bundle-plugin (v. 2.5.3 at the time of writing) has its own go at resource filtering after the resource plugin is done. If the resource plugin cannot replace a property it will simply leave it as it is. Which is what you want, of course, if the property is in a Spring context file to be replaced by Spring at run-time. But the left-over properties confuse the bundle plugin.
The only way around this that I could find was to disable resource filtering for the Spring context file. In the build section of your POM add something along the lines of this:
<resources>
<!-- globally enable resource filtering -->
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<!-- then disable it for specific resources -->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*-context.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
You can also turn it around and explicitly include your to be filtered files in a resource declaration that enables filtering and globally disable filtering.

Related

maven resources plugin - How to filter from external file?

I need to configure some log4j2.xml file based on some properties and those properties vary according to the environment. For example I set log.org.hibernate=info for development and log.org.hibernate=error for production etc.
I'm using maven resources plugin as shown below and it's working fine.
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<filters>
<filter>${project.basedir}/src/main/resources/env/prod.properties</filter>
</filters>
Now I need to filter external properties file and set those properties to the same log4j2.xml file. But AFAIK resources plugin doesn't allow external file filtering.
How can I read the properties from external properties file (outside of my project directory) and set them to an inner resource (such as log4j2.xml file)?

How to acces POM project name from properties file

I have logback.properties file which contains the following properties.
#logging
logging.server=
logging.port=
logging.path=/opt/${project.name}/logs
Inside my POM file I've got the <name> tag, which specifies the project name. I'd like to inject this name in few properties files like the lockback one above. Doing the above results in creating a folder called project.name_IS_UNDEFINED. Is it possible to access the project name and how?
UPDATE
Ralph gave the correct answer, however check my comment, because for spring boot applications you need to used %project.name% instead of ${project.name}!
You need to transfer the properties defined in you maven compile-time script into the application at run time.
The most easiest way is to use maven's resource filtering (the name is a bit misleading, it is not a filter that selects files, it is a property replace for text/resource files) in order to let maven replace ${project.name} in you logback.properties file.
true
${basedir}/src/main/resources
If you want enabling resource filtering just for one file (and not for the others to prevent maven from replacing other markers then you can use this snippet:
<resources>
<!-- enable filtering for logback.properties only -->
<resource>
<filtering>false</filtering>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
<excludes>
<exclude>WHEREVER_LOCATED/logback.properties</exclude>
</excludes>
</resource>
<resource>
<filtering>true</filtering>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>WHEREVER_LOCATED/logback.properties</include>
</includes>
</resource>
</resources>
Try with the resource filtering:
https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html

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™.

How to access maven.build.timestamp for resource filtering

I am using maven 3.0.4 and would like to make the build timestamp accessible to my application. For this, I'm putting a placeholder in a .properties file and let maven filter on build. While this is working fine for ${project.version}, ${maven.build.timestamp} is not substituted on filtering.
The property seems to be available on build - I can use it to modify the artifact name:
<finalName>${project.artifactId}-${maven.build.timestamp}</finalName>
So why is it not available for resource filtering? And, more importantly, how do I make it accessible?
I have discovered this article, explaining that due to a bug in maven, the build timestamp does not get propagated to the filtering. The workaround is to wrap the timestamp in another property:
<properties>
<timestamp>${maven.build.timestamp}</timestamp>
<maven.build.timestamp.format>yyyy-MM-dd HH:mm</maven.build.timestamp.format>
</properties>
Filtering then works as expected for
buildTimestamp=${timestamp}
I can confirm as of Maven 3.x {maven.build.timestamp} is "working" now. They work arounded the problem, apparently. No additional properties workaround needed anymore.
However, be careful your "filtering" plugin (maven-resources-plugin) is up to date. It needs to be relatively new, so if mvn help:effective-pom shows an old version (ex: 2.6), bump it to something newer, fixed it for me, 3.x ex:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<properties><timestamp>... workaround is no longer required...
This also cleared up, kind of, why it was working in IntelliJ but not the command line. IntelliJ probably uses their own "modified/internal" maven constants, so it was working there, but not from maven command line.
Also note if you add a filtering resource directory to you pom, you may need to also "re-add" the default directory, it gets lost, ex:
<resource>
<directory>src/main/resources-filtered</directory> <!-- to get "maven.build.timestamp" into resource properties file -->
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory> <!-- apparently have to add this is you have the other... -->
</resource>
NB if you're using spring boot as your parent, you have to use #maven.build.timestamp# instead. Also note if you're using spring boot there's a file META-INF/build-info.properties that is optionally created by the spring-boot-maven-plugin that you can read (spring provides a BuildProperties bean for convenience reading it).
In order to enrich the Stackoverflow content for others, that like me, found this post as a way to solve the "problem" of ${maven.build.timestamp}. This is not a maven bug, but an expected behavior of m2e, as can be seen in this post.
Therefore, I believe that we can not expect the solution to be "corrected", since, from what I understand, the correction involves conceptual issues.
In my case, what I did was use the plugin (buildnumber-maven-plugin) as described in this other post.
Adding Maven properties at the pom project level doesn't take into account correct local Timezone, so timestamp may appear wrong :
<properties><timestamp>${maven.build.timestamp}</timestamp></properties>
Using the build-helper-maven-plugin applies the correct timezone and current daylight saving to the timestamp :
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<id>timestamp-property</id>
<goals>
<goal>timestamp-property</goal>
</goals>
<configuration>
<name>timestamp</name>
<pattern>yyyy-MM-dd HH:mm:ss</pattern>
<timeZone>Europe/Zurich</timeZone>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
When packaging, Maven will replace any token timestamp in /resources folder, e.g. resources/version.properties :
build.timestamp=${timestamp}
You can then load this properties file in your Application.

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>

Resources