Finding files by pattern in Maven - java

I need to pre-process some of the resource files (*.xsl) in a Java Maven project.
I found the exec-maven-plugin which is able to execute shell commands and thought I can do it in 2 steps:
match a list of files and write it to a file
launch a Java program that pre-processes each file from the list
However I don't get any output from the find command I am trying to execute. The plugin configuration in POM looks like this:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>inline-xml-entities</id>
<phase>generate-resources</phase>
<configuration>
<executable>find</executable>
<useMavenLogger>true</useMavenLogger>
<outputFile>xsl-files.txt</outputFile>
<arguments>
<argument>./src/main/webapp/static/com/atomgraph/</argument>
<argument>-type</argument>
<argument>f</argument>
<argument>-name</argument>
<argument>'*.xsl'</argument>
</arguments>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>
I can see in the mvn clean install -X log that the command gets executed:
[DEBUG] Executing command line: [find, ./src/main/webapp/static/com/atomgraph/, -type, f, -name, '*.xsl']
The xsl-files.txt file gets created, but it's empty :/
If I go to the project basedir and execute find ./src/main/webapp/static/com/atomgraph/ -type f -name '*.xsl' manually, I get a list of .xsl files as expected:
./src/main/webapp/static/com/atomgraph/linkeddatahub/xsl/bootstrap/2.3.2/admin/acl/imports/acl.xsl
./src/main/webapp/static/com/atomgraph/linkeddatahub/xsl/bootstrap/2.3.2/admin/acl/layout.xsl
./src/main/webapp/static/com/atomgraph/linkeddatahub/xsl/bootstrap/2.3.2/admin/layout.xsl
...
What am I missing?

This seems to have worked:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>find-xsl-files</id>
<phase>generate-resources</phase>
<configuration>
<executable>find</executable>
<workingDirectory>src/main/webapp/static/com/atomgraph/</workingDirectory>
<outputFile>xsl-files.txt</outputFile>
<commandlineArgs>. -type f -name '*.xsl'</commandlineArgs>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>

Related

JaCoCo tests run on a jar file from pom.xml

I am trying to get the coverage of a few test cases related to an open source project of which I got the jar file.
I'm using maven as automatic build tool and the jacoco plugin for maven to get the coverage of the tests on the project.
What I would like to achieve is the same output that I can get with the command:
-jar jacococli.jar report jacoco.exec \
--classfiles <project.jar> \
--sourcefiles <myTestsDirectory> \
--html <jacocoReportsDirectory> \
--xml <jacocoReportsDirectory>coverageFile.xml \
--csv <jacocoReportsDirectory>coverageFile.csv
When I run this from the commandline I can retrieve the report for the coverage computed on the whole project (the jar).
I don't know how to insert this command (or something that can get me the same output) in the pom. I tried to put it in the surefire plugin argLine (with the -javaagent command) but this got me nowhere.
This is where I got so far:
<build>
<sourceDirectory>src</sourceDirectory>
<testSourceDirectory>src/test</testSourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.7</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>target/coverage-reports/jacoco.exec</dataFile>
<outputDirectory>target/coverage-reports/jacoco-reports</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<argLine>
-javaagent:lib/jacocoagent.jar=destfile=target/coverage-reports/jacoco.exec
</argLine>
<reportsDirectory>target/coverage-reports/surefire-reports</reportsDirectory>
</configuration>
</plugin>
</plugins>
</build>
Giving the whole src directory as the sourceDirectory gets my test cases as the target of the coverage computation and I don't want that.
Note: there are no java classes in src to which the tests are targeted, so there's no need to put that as sourceDirectory. I assume this has to point to the jar somehow.

properties-maven-plugin not working for goal write-project-properties

I need to export property values from mvn command line to a file which is needed for my java code later. But i always get the error:
[ERROR] Failed to execute goal
org.codehaus.mojo:properties-maven-plugin:1.0-alpha-2:write-project-properties
(default-cli) on project MyApplication: The parameters 'outputFile'
for goal
org.codehaus.mojo:properties-maven-plugin:1.0-alpha-2:write-project-properties
are missing or invalid -> [Help 1] [ERROR]
Here is my project structure
MyApplication
|src/main/java
|src/main/resources
|src/test/java
|src/test/resources
|lib ////(no files here yet)
|pom.xml
And my pom.xml is:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>write-project-properties</goal>
</goals>
<configuration><outputFile>${basedir}/lib/build.properties</outputFile>
</configuration>
</execution>
</executions>
</plugin>
Note: I tried by manually creating empty file build.properties in lib folder and even same error. Tried with plugin version 1.0.0 too.
OK, after hours of trying different combinations, (moved <configuration> above/out of <executions>)in pom.xml, i see now lib/build.properties file created. But can someone explain this please?
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<configuration>
<outputFile>${basedir}/lib/build.properties</outputFile>
</configuration>
<executions>
<execution>
<id>write-project-properties</id>
<goals>
<goal>write-project-properties</goal>
</goals>
<phase>test</phase>
</execution>
</executions>
</plugin>

cannot run npm grunt bower using maven

I have a simple web application that uses npm bower and grunt. I am using this project as a module in a maven project. I searched the internet and found how to define the pom.xml for the project but I am not able to run it. Can anyone tell me the steps on how to build and run the webapp using maven.
the pom.xml
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.5.0</version>
<executions>
<execution>
<id>exec-npm-install</id>
<phase>generate-sources</phase>
<configuration>
<executable>npm</executable>
<arguments>
<argument>install</argument>
</arguments>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
<execution>
<id>exec-bower-install</id>
<phase>generate-sources</phase>
<configuration>
<executable>bower</executable>
<arguments>
<argument>install</argument>
</arguments>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
<execution>
<id>exec-grunt</id>
<phase>process-resources</phase>
<configuration>
<executable>grunt</executable>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
The error that I am getting is
[ERROR] Command execution failed.
java.io.IOException: Cannot run program "C:\Program Files\nodejs\npm" (in directory "C:\Users\krs\IdeaProjects\project"): CreateProcess error=193, %1 is not a valid Win32
application
How to build and run this pom using maven ?
The reason why you can't run it is because it's not an executable, it's a batch file or shell script if you're not on windows.
You can still use maven exec plugin to run it. However to do that you'll have to feed the batch file npm to cmd program (or bash or whatever is your favorite shell).
Following is the change that you'll need to make.
Actual command to feed batch to cmd
cmd /c "npm --version"
Following is the change in the plugin configuration.
<configuration>
<executable>cmd</executable> <!-- or bash -->
<workingDirectory>./</workingDirectory>
<arguments>
<argument>/c</argument>
<argument>"npm --version"</argument>
</arguments>
</configuration>
This should work.

How to Rmic and Rmiregistry

I need to know how can I run Rmic. I have programmed my app in Java Eclipse and now I need to run Rmic. I tried it by running CMD, changing directory to Bin folder of my eclipse project and then typing command Rmic class name. It shows error unrecognised command. Is I am doing something wrong?
I use Maven and this is what I have in my pom file. It works quite well.
<groupId>org.codehaus.mojo</groupId>
<artifactId>rmic-maven-plugin</artifactId>
<executions>
<execution>
<id>rmi package</id>
<phase>package</phase>
<goals>
<goal>package</goal>
</goals>
<configuration>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<includes>
<include>**/*_Stub*</include>
</includes>
</configuration>
</execution>
<execution>
<id>rmi compilation</id>
<goals>
<goal>rmic</goal>
</goals>
<configuration>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
</configuration>
</execution>

How to create JavaDoc documentation by using command mvn install?

I have a question concerning the maven javadoc plugin? I have configured that plugin with this values:
<build>
....
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9</version>
<configuration>
<noqualifier>all</noqualifier>
<reportOutputDirectory>${basedir}/MyDoc/javadoc</reportOutputDirectory>
<destDir>javadoc</destDir>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>javadoc</goal>
</goals>
</execution>
</executions>
</plugin>
...
</build>
Is there a way to create some kind of documentation, if I use the command mvn clean install? I donĀ“t want to create a Jar File with my JavaDoc documentation, I need a way to create the JavaDoc and put the created source file directly in my maven project.
Thanks !
Greetz
Marwief
To execute plugin during certain phase, add <phase> to <execution>. Plugin should be fired:
<executions>
<execution>
<id>attach-javadocs</id>
<phase>install</phase> <------ HERE
<goals>
<goal>javadoc</goal>
</goals>
</execution>
</executions>
More on maven lifecycle here

Categories

Resources