How to retrieve <finalName> value from POM? - java

I manually set the tag value in my pom.xml before I packaged my Spring web app to .war and then I manually deploy my .war file on JBOSS.
I have an test.html file in webapp folder which is sending an POST request to test if #RequestMapping methods inside my controller class are working as expected or not. I want to store the value of tag in the JavaScript variable (in test.html).
How can I fetch the value from pom.xml and store in the JavaScript variable?

Make a filtered property resource file that has ${finalName} in it, and then read that in from your classpath.
src/main/resources/config.properties:
finalName=${propContainingFinalName}
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>config.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>

Related

Getting version from pom in JSP for Spring Boot Web

I was trying to print the application version from pom.xml in my JSP pages. i tried combing the filtering with Spring boot.
But the values are not rendered in the jsp pages.
I added the below section in pom.xml.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<webResources>
<resource>
<directory>src/main/resources/templates</directory>
<filtering>true</filtering>
</resource>
</webResources>
</configuration>
</plugin>
And addded the below in jsp pages:-
<h4>${project.description}</h4>
<h4>${project.name}</h4>
<h4>${project.version}</h4>
But when the JSP page renderes the tags are empty with no values in it.
I was trying to solve very same issue: basically to insert value of project variables statically into JSP generated at build time. Without additional call to get properties etc at run time. Here is a snippet that solved it for me.
I am using spring and it is using maven-war-plugin underneath.
I put all files that I need static values into subfolder so not all jsps are filtered. Later I static include such files.
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resource>
<directory>src/main/webapp/WEB-INF/jsp/version/</directory>
<filtering>true</filtering>
<targetPath>WEB-INF/jsp/version/</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
As described in the documentation, Spring Boot uses #…# as the delimiter rather than ${…}:
since the default config files accept Spring style placeholders (${…​}) the Maven filtering is changed to use #..# placeholders (you can override that with a Maven property resource.delimiter)
This means that your JSP needs to contain the following:
<h4>#project.description#</h4>
<h4>#project.name#</h4>
<h4>#project.version#</h4>
Also, AFAIK, filtering for src/main/resources/templates should be enabled like this:
<resources>
<resource>
<directory>src/main/resources/templates</directory>
<filtering>true</filtering>
</resource>
</resources>

How to use pom.xml property in junit test class

I have declare a property in pom.xml
<PROPERTY_KEY>d:/../.../abc.properties</PROPERTY_KEY>
and then used
<plugin>
<configuration>
<replacements>
<replacement>
<token>APP_PROPERTY</token>
<value>${PROPERTY_KEY}</value>
....
</plugin>
and have used APP_PROPERTY in my dispatcher-servlet.xml and in controller classes as well. It's working fine as in this case control comes through web.xml and this web.xml has an entry for my dispatcher-servlet.xml.
But when I want to use this same APP_PROPERTY in my JUNI test class, it is not getting resolved.
I have to create a new dispatcher-servlet-test.xml file (and put it under /src/main/resources folder) as my actual dispatcher-servlet.xml is not working from JUnit test class. Now my JUnit test class is able to pick dispatcher-servlet-test.xml. But, it is not able to resolve properties (APP_PROPERTY) that I have defined in my pom.xml.
I'm using SpringJUnit4ClassRunner.class in my test class.
What should I do to get these property resolved?
Resources for tests need to live under src/test/resources, you should move your dispatcher-servlet-test.xml there. Once moved, it's likely not being filtered as a resource, the stanza you want is something like:
<build>
<resources>
<resource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/dispatcher-servlet-test.xml</include>
</includes>
</resource>
You may already have a similar stanza in your POM, or if you're working on a larger project the parent pom.xml may have a stanza declaring:
<filtering>true</filtering>

Replace test property values at the time of maven build

I have a test.properties file located in: $PROJECT_HOME/src/test/resources with the following content:
hostname=${host}
I also have a main.properties file located in: $PROJECT_HOME/src/main/resources with the same content.
Then I specified the followinging lines in the pom-file of my project.
<properties>
<host>localhost</host>
</properties>
<build>
<resources>
<!-- Filter resource files -->
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
....
</build>
After executing a mvn clean install I see that the main.properties in my target folder is replaced with the localhost value. However the property in my test.properties is not...
My first idea was to adapt the resources as this:
<properties>
<host>localhost</host>
</properties>
<build>
<resources>
<!-- Filter resource files -->
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
....
</build>
When building now, the test.properties file is replaced with the localhost value, but is placed into the classes folder in the target. In test-classes, there is still the test.properties file without the replaced value...
Is there a way to also replace the value in the test-classes folder?
My idea is to work on my local server with the localhost value without specifying it as a parameter and to overwrite this value with an alternate host when performing integration tests against a test server. In this case I specify the value -Dhost=<> on our continuous integration system.
This should work (although test.properties will not end up in your classes folder and test-classes would have both test.properties and main.properties with the replaced value which I think was what you wanted but wasn't sure).
<resources>
<!-- Filter resource files -->
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<testResources>
<!-- Filter test resource files -->
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
Also, you may want to have two properties, ${host} (referenced in host.properties) and ${test.host} (referenced in test.properties), otherwise if you specify -Dhost during an integration build it will replace the value in both the host.properties and the test.properties. This way you can specify -Dtest.host to change which host you are using for integration tests but not the host that is set when you deploy.

How to include python files of Java project into .war with maven?

I have Java project made with maven. So I have typical maven project layout. And I use Jython. So I got few python files. Wich I use through PythonInterpreter in Java classes.
I place my python files in src/main/py folder. And I use this path to import the modules by interpreter. It works fine on my laptop.
The problem is:
When I do mvn install, this folder does not goes to the war.
I read about maven resources plugin and added this folder as a resource. Like this:
<resources>
<resource>
<directory>src/main/py</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
I that case it adds everything that folder contents, to web-inf/ directly, but not in the src/main/py. So that path is invalid for application in war archive.
Question is:
How should I place this python resource and what I should write in pom.xml, to be able to use the same path on the laptop, and the server?
Can you try including targetPath in the resource element as below:
<resource>
<targetPath>../</targetPath>
<directory>src/main/py</directory>
<includes>
<include>**/*.py</include>
</includes>
</resource>
I would suggest to use the src/main/scripts folder and check if they where packaged into the war if this does not work check the documentation of the maven-war-plugin and define it with the war plugin as described in the documentation.
<resources>
<resource>
<directory>src/main/py</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
is actually enough to include into jar/war file.
For running see
Compile Python Sources in Maven
It is not recommended to use src/main/py in your python script,
think as src/main/py as working/current folder for you python

Preventing context XML from being override in Tomcat 6

I am using JNDI to read database configuration from my application's context.xml. The way I currently have this setup is to have [appname].xml in conf/Catalina/localhost. However, when I redeploy the app, this file gets overridden with an empty context file, and I have to copy the custom one back to the conf/Catalina/localhost directory. I have different database settings, etc. for my test and production servers, and so don't want to put the context file in META-INF in the WAR file, but would like to just keep it in the conf/Catalina/localhost directory. Is this possible?
Is there somewhere better to put the database configuration?
I'd also like to avoid putting the configuration in the server.xml file, although I know this is possible.
Thanks!
I would say look into using maven profiles (one for prod, one for test), and having different resource definitions for each profile. You can keep your common files in src/main/resources and then have a folder for each profile type to keep specific config files in:
src/test/resources
src/prod/resources
Then you can amend your pom to define each profile and its associated resources:
<project>
<profiles>
<profile>
<id>prod</id>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<targetPath>${project.build.outputDirectory}</targetPath>
</resource>
<resource>
<directory>src/prod/resources</directory>
<targetPath>${project.build.outputDirectory}</targetPath>
</resource>
</resources>
</build>
</profile>
<profile>
<id>test</id>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<targetPath>${project.build.outputDirectory}</targetPath>
</resource>
<resource>
<directory>src/test/resources</directory>
<targetPath>${project.build.outputDirectory}</targetPath>
</resource>
</resources>
</build>
</profile>
</profiles>
</project>
finally you can build the war using the -Pprod or -Ptest profile argument to mvn
mvn -Pprod package
Problem is that undeploy removed the webapp specific context.xml file that was installed in Catalina/localhost/.xml
If you don't want to have the file removed, you'll have to just redeploy it, not undeploy/deploy

Categories