Read a file into a Maven property - maven

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.

Related

How to specify output format for jacoco plugin for maven?

I have a maven project with jacoco plugin, which generates reports in different formats, such as html, csv and xml. But I need only html. How can I specify it?
Here is some code, where I add jacoco plugin:
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.plugin.version}</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
//other plugins
</plugins>
</build>
Then I run tests:
mnv clean test
And all reports appears in "target" directory.
I read documentation https://www.eclemma.org/jacoco/trunk/doc/maven.html, but I didn't found anything about how to pick requiring format. I found it only for Ant and Gradle.
I suppose I missing something, so I will be grateful for any clue.
I searched the same thing and stumbled over the following issue with it:
The old path was
target\jacoco\jacoco.report
The XML report however is put into:
target\site\jacoco
and in there are the XML, csv, html ...
As of today report goal of jacoco-maven-plugin unconditionally generates XML, HTML and CSV - see https://github.com/jacoco/jacoco/issues/9
And in my opinion there is no reasons to disable HTML and XML - cost of generation is small, developers can view HTML in place, while XML consumed by other tools such as SonarQube or Jenkins.
As a workaround if highly needed, report task of JaCoCo Ant Tasks can be executed via maven-antrun-plugin.
You can specify the report format to generate by specifying the configuration as follows-
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.7</version>
<configuration>
<!-- Rest of your configuration, if any -->
<formats>HTML</formats>
</configuration>
</plugin>
Note that this is available since version 0.8.7. Useful resource- https://www.jacoco.org/jacoco/trunk/doc/report-integration-mojo.html#formats

How to Read a Property File in a Maven Project?

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.

Are directions available for the maven plugin com.alexnederlof jasperreports-plugin?

I have read up on the limited information regarding com.alexnederlof jasperreports-plugin and I'm looking to convert my current ant build to use this maven plugin, but there doesn't seem to be any documentation available.
My biggest concern is run-time: If I use this plugin at build-time, what version of jasper-reports do I need to use at run-time?
Am I missing a reference somewhere? As the old adage goes, "If there isn't any documentation, then I guess I'll have to write it."
I am not sure of what you are after but, I am using this plugin in maven to generate the source .jrxml files to .jasper files and the configuration in pom goes like this:
<plugin>
<groupId>com.alexnederlof</groupId>
<artifactId>jasperreports-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>jasper</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- These are the default configurations: -->
<compiler>net.sf.jasperreports.engine.design.JRJdtCompiler</compiler>
<sourceDirectory>src/main/jasperreports</sourceDirectory>
<outputDirectory>${basedir}/src/main/webapp</outputDirectory>
<outputFileExt>.jasper</outputFileExt>
<xmlValidation>true</xmlValidation>
<verbose>false</verbose>
<numberOfThreads>4</numberOfThreads>
<failOnMissingSourceDirectory>true</failOnMissingSourceDirectory>
<sourceScanner>
org.codehaus.plexus.compiler.util.scan.StaleSourceScanner
</sourceScanner>
</configuration>
</plugin>
Hope this helps

Can I get Maven to turn a file into properties I can use in my Maven script?

I’m using Maven 3.1.0. I have a file like so
driver: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/db
username: user
password: pass
Is there a way I can get Maven to parse this file so that I can use the keys (e.g. “username”) as properties (e.g. ${username}) in other parts of my Maven script? Ultimately I want to pass these properties as connection parameters to the Maven SQL plugin.
Unfortunately, its not an option to change the format of this file.
Take a look at the Properties Maven Plugin:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>myconfig.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
The plugin should be able to handle your properties file. if not (format problems), you could include a build step to convert the file to a more convenient format.
EnvInject Plugin used to inject Variables from property files. Refer https://wiki.jenkins-ci.org/display/JENKINS/EnvInject+Plugin

Maven exec bash script and save output as property

I'm wondering if there exists a Maven plugin that runs a bash script and saves the results of it into a property.
My actual use case is to get the git source version. I found one plugin available online but it didn't look well tested, and it occurred to me that a plugin as simple as the one in the title of this post is all I need. Plugin would look something like:
<plugin>maven-run-script-plugin>
<phase>process-resources</phase> <!-- not sure where most intelligent -->
<configuration>
<script>"git rev-parse HEAD"</script> <!-- must run from build directory -->
<targetProperty>"properties.gitVersion"</targetProperty>
</configuration>
</plugin>
Of course necessary to make sure this happens before the property will be needed, and in my case I want to use this property to process a source file.
I think you could utilize gmaven plugin for this task:
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<properties>
<script>git rev-parse HEAD</script>
</properties>
<source>
def command = project.properties.script
def process = command.execute()
process.waitFor()
project.properties.gitVersion = process.in.text
</source>
</configuration>
</execution>
</executions>
</plugin>
After execution of this script you should be able to refer to ${gitVersion} property.

Resources