How to Read a Property File in a Maven Project? - maven

I've read all the related entries on this topic but nothing is working.
I have placed the database property file under resources/property/db.properties, added the following to Maven:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>write-project-properties</goal>
</goals>
<configuration>
<outputFile>${project.build.outputDirectory}/property/db.properties</outputFile>
</configuration>
</execution>
</executions>
</plugin>
and using the following code to get the stream:
this.getClass().getClassLoader().getResourceAsStream("db.properties");
but when the Properties object loads it, it strangely only contains a single property defined in pom.xml (which is an encoding property)!
I have tried the following as well at no avail:
this.getClass().getClassLoader().getResourceAsStream("property/db.properties");
I can see that the file is at target's class/property/db.properties (or in latter case, in class/db.properties) when the project gets built and deployed (Tomcat). Is there something I am missing here?
UPDATE
As it is suggested in the comment below, use "/" to prefix the path to the property file.
If you want to do it using properties-maven-plugin, make sure property values are indicated in Properties section of POM.xml. This plugin then grabs these values and puts them in a property file indicated by outputFile tag.

Related

Maven Antlr3 plugin generates code in weird location

I am using plugin and Antlr version 3.3 for a project under H:/compiler
I have a tokens file under my src/main/antlr3/com/cbc/example directory called CBCTokens.g. In the same package i have a parser grammar file called MyScribe.g that references the tokens using tokenVocab=CBCTokens. I also have a tree grammar in the same directory.
However when i try to execute the build, i get an error on the very first file the plugin encounters saying:
Error(1): cannot write file : java.io.FileNotFoundException: H:\compiler\target\generated-sources\antlr3\H:\compiler\src\main\antlr3\CBCTokensLexer.java (The filname, directory name, or volume label syntax is incorrect)
It seems to me that the plugin is determining the output path using some weird combination of baseDir and the default output directory.
What configuration am i missing?
Thanks
First best thing is never to generate code into src/main/ folder whatever. This means for your configuration just remove <outputDirectory> tag and remove the <sourceDirectory> cause it's the default of the antlr3-maven-plugin.
<build>
<plugins>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr3-maven-plugin</artifactId>
<version>3.3</version>
<configuration>
<printGrammar>false</printGrammar>
<verbose>true</verbose>
</configuration>
<executions>
<execution>
<goals>
<goal>antlr</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

jaxb2-maven plugin

I wanna generated java classes from xsd files bt soome how whenever i run the code it shows the error
No Schema has been found... here is the code... Kindly help...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaDirectory>src/main/resources/xsd</schemaDirectory>
<includeSchema>**/*.xsd</includeSchema>
<!-- <generatepackage>org.onesync.esb.datasync.model</generatepackage> -->
<!-- The package in which the source files will be generated. -->
<packageName>org.onesync.esb.datasync.model</packageName>
<!-- The working directory to create the generated java source files. -->
<outputDirectory>src/main/java/org/onesync/esb/datasync/model</outputDirectory>
</configuration>
</plugin>
</plugins>
I don't think <includeSchema>**/*.xsd</includeSchema> is valid syntax for jaxb2-maven-plugin:xjc Try omitting that parameter.
If you don't specify schemaFiles it should use all XSD files in the schemaDirectory.
"schemaFiles -- List of files to use for schemas, comma delimited. If none, then all xsd files are used in the schemaDirectory. This parameter also accepts Ant-style file patterns." (see jaxb2-maven-plugin documentation)
BTW, it is usually a good idea to use maven's configuration parameters to refer to a directory. For example, change <schemaDirectory>src/main/resources/xsd</schemaDirectory> to <schemaDirectory>${project.basedir}/src/main/resources/xsd</schemaDirectory>.
Finally, you might also want to refer to this similar SO post.
I am also using maven configuration and spent almost to compile the project stuff. Later on i came to know that it looking for the schema file i.e. schema.xsd.
If you are using the MAVEN configuration then by default you can put the schema file under the resources directory.
But if you want to specify your path for finding the schema file then you can use the includeSchema tag of schemaDescription in plugin configuration.
OR
You can use the effictive pom to search for specific tag also.
Command for effective pom in maven: mvn help:effective-pom
Thanks

Read a file into a Maven property

Is it possible to read a file and store the entire contents into a Maven property?
I'm trying to generate some custom JavaDocs, and they have an option to set a header, but it has to be a string, not a file name (for some reason). I obviously don't want to put a bunch of HTML into my pom.xml, so I need a way to read my header.html as a Maven property.
Is this possible? I'm not able to find a standard way, but it seems like a Maven plugin might do this.
Apparently Ant has what I want, but I'd prefer something lighter weight than the Ant task.
See this question on SO. And here is the properties-maven-plugin.
If you'd like not to use .properties file, than I can suggest to use the Groovy plugin and a small script that I've written:
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<properties>
<header_file>header.html</header_file>
</properties>
<source>
def file = new File(project.properties.header_file)
project.properties.header_content = file.getText()
</source>
</configuration>
</execution>
</executions>
</plugin>
After execution of this plugin, you should be able to refer to ${header_content} property that contains header.html file contents.

get maven to replace values based on user/build.properties

im at the point in my project where im moving data connections to the beta and production databases for testing. obviously, having the alpha database credentials stored in the source repository is fine, but the beta and production credentials, id be put in front of a firing squad for that one.
i know maven can have a {userdir}/build.properties file. this is the file i want to use to keep the db credentials out of the source repository. but i can't seem to get maven to figure out that for file x.cfg.xml it has to replace values.
so i have in one of my hibernate.cfg.xml files this line
<property name="hibernate.connection.url">#ssoBetaUrl#</property>
now how do i get maven to replace that variable with the value thats in the {userdir}/build.properties file?
edit-------------
ive been playing with the properties-maven-plugin plugin but i seem to not be able to get it to fire. i put this in my parent pom
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<id>read-properties</id>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
</execution>
</executions>
</plugin>
but when it builds, it does not fire. if im reading http://maven.apache.org/maven-1.x/reference/properties.html right it should find the build properties file in the ~/build.properties folder and go from there, but im not sure.
I think you are approaching this the wrong way around. Instead of having the build process bake the appropriate connection details into the JAR file you should instead have the program look for a configuration file at startup.
Typically, my hibernate based apps, will look for a file under %user.home&/.appname/config.properties and load DB credentials and other deployment specfic data from there. If the file is missing, a default version can be included in the JAR and copied to this location (on initial startup so you don't have to copy-paste the file to new systems) that is then edited with appropriate settings.
This way, you can use the same build to produce JAR (or WAR) files for test and production servers, the differences will be in the (presumably already deployed) configuration files. This also makes it possible to have multiple production deployments, each talking to a different database, without any complications in the build process.
You could use two plugins.
properties-maven-plugin
replacer
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-1</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>{userdir}/build.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.2</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>target/**/*.*</include>
</includes>
<replacements>
<replacement>
<token>#ssoBetaUrl#</token>
<value>http://[anyURL]</value>
</replacement>
</replacements>
</configuration>
</plugin>

Buildnumber not getting propagated down to childern from parent

I am using the buildnumber-maven-plugin in my parent pom to create a build number. This works fine. However, I need to have the ${buildNumber} property in my child. I have added antrun ECHO for that property in my child and it is not there. How can I propagated that property down to children?
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<inherited>false</inherited>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
<configuration>
<buildNumberPropertiesFileLocation>buildnumber.properties</buildNumberPropertiesFileLocation>
<format>{0,number,integer}</format>
<items>
<item>buildNumber0</item>
</items>
</configuration>
</plugin>
I found the issue. This may be a bug in the build number plug in. The property I use for the build number (the default name or one I set) does not get propagated down to the child project unless I add in
<getRevisionOnlyOnce>true</getRevisionOnlyOnce>
After adding this line in my pom for the build number plugin, all worked fine. Seems like if you don't have this line in the pom for that plugin it whips out the build number property upon completion of the Parent. Not sure. But all is working now.
Try to remove
<inherited>false</inherited>

Resources