Using Maven resource plugin on dependecy jar file - java

Maven has resource plugin that can delimiter a character that can tell it in pom file , for example in the blow config define it that replace "#" character.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>target/filtered-resources/scripts</outputDirectory>
<resources>
<resource>
<directory>src/assemble/resources/scripts</directory>
<filtering>true</filtering>
</resource>
</resources>
<useDefaultDelimiters>false</useDefaultDelimiters>
<delimiters>
<delimiter>#</delimiter>
</delimiters>
</configuration>
</execution>
</executions>
</plugin>
but it work on files that directly exists in my project , i want to do the same job in a jar file , i have a jar file that exists some java script plugins and i want add it to my project but i need replace somethings on it .
and the dependency is something like this :
<dependency>
<groupId>sample.javascript</groupId>
<artifactId>jsLibrary/artifactId>
<version>1.0.0</version>
</dependency>

1) you need to unpack your jar. You can use this plugin: https://maven.apache.org/plugins/maven-dependency-plugin/examples/unpacking-artifacts.html
2) change
<directory>src/assemble/resources/scripts</directory>
to the directory with unpacked jar

Related

Include a zip file in the Maven generated jar

I need to include a zip file in the src/test/resource dir in the maven generated jar .
Any idea how to do that?
As suggested in isapir's answer, you should ideally place the zip file under src/main/resource if that needs to be included in your jar file. But if you really need to include it from your src/test/resource folder, you can use build-helper-maven-plugin as below:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-resource</id>
<phase>generate-resources</phase>
<goals>
<goal>add-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<include>ABC.zip</include>
<directory>src/test/resource</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
You should put the zip archive in the src/main/resources directory and not in src/test/resource.
If you want to have more control then check out the Maven Resources Plugin documetation: https://maven.apache.org/plugins/maven-resources-plugin/index.html

how to update a property file using maven or pom.xml

I have created an automation framework where I am reading values from a property file say "config.properties".
My config.propertioes file contains following :
BrowserName=${browser}
Environment=${env}
I am reading browser value from the property file and passing it to my selenium script to run it.
Now I wants to replace "${browser}" && "${env}" with value "ie" && "IT" using pom.xml. Is there any way/plugin using which I can edit a property file using pom.xml.
Please suggest.
#Keshava
I am putting whole example below as suggested below :
1.Have 2 property files: ◦project.properties: This is the file that we commit in the repository. It consists data as follows: ◾project.connection.username=##DB_USERNAME##
project.connection.password=##DB_PASSWORD##
◦build.properties: This is the file that we do not commit in the repository and is maintained at each of the deployment environments, be it the developers env., UAT env or Production env. The contents of this file are as follows: ◾db.username=mydbuser
db.password=mydbpassword
2.In the project’s pom.xml add the following plugin and a execution:
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>maven-replacer-plugin</artifactId>
<version>1.3.5</version>
<executions>
<execution>
<id>replaceTokens</id>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<file>target/classes/project.properties</file>
<replacements>
<replacement>
<token>##DB_USERNAME##</token>
<value>${db.username}</value>
</replacement>
<replacement>
<token>##DB_PASSWORD##</token>
<value>${db.password}</value>
</replacement>
</replacements>
</configuration>
</plugin>
from above, I understand that "##DB_USERNAME##" is from "project.properties". But, from which properties file this "${db.username}" value will be taken?. how my pom will understand from where to take "${db.username}" .
Do I need to pass this value in maven goal like below :
mvn clean install -Ddb.username=myuserid
Hello you can use the maven resource plugin.
This plugin implement "Maven Filtering".
<project>
...
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>selenium-profile-chrome</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/selenium</outputDirectory>
<resources>
<resource>
<directory>src/non-packaged-resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
...
</build>
...
</project>
you could tryin using the maven replacer plugin
See https://code.google.com/archive/p/maven-replacer-plugin/
See an example here

maven - how to compile code to jar file and not .class

I am trying to build a groovy project using maven. My packaging type is war file. Maven is building the project and putting all dependent libraries in WEB-INF/lib folder but it is compiling all code into class files and putting it into WEB-INF/classes folder. Is there a way I can tell maven to build jar file for my project also and put it into WEB-INF/lib folder.
My pom.xml looks like this :
<groupId>com.myproject</groupId>
<artifactId>ExampleProject</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>My Example Project</name>
<url>http://maven.apache.org</url>
<dependencies>
...
...
...
</dependencies>
<build>
<resources>
<resource>
<directory>${basedir}/src/main/groovy</directory>
<excludes>
<exclude>**/*.groovy</exclude>
</excludes>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/groovy</source>
<source>src/main/resources</source>
</sources>
</configuration>
</execution>
<execution>
<id>add-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/test/groovy</source>
<source>src/test/resources</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<finalName>master</finalName>
</build>
In these scenarios the usual approach is to you separate your library code in a different module that will be a dependency from your war module. For this suggestion you can see also how to generate jar and war both in project .
However if you still prefer to go with the solution you mention, you can do it with the following configuration in your pom
<configuration>
..
<attachClasses>true</attachClasses>
<archiveClasses>true</archiveClasses>
</configuration>
(see http://maven.apache.org/plugins/maven-war-plugin/war-mojo.html, and how to use class file from another war)

How to put maven zip dependency on classpath for Java tests

I have a Java project entirely consisting of junit/integration tests which is managed by maven. One of the dependencies is a zip archive, the contents of which I would like to be available on the classpath when the tests are run. Since maven does not put the content of a zip dependency on the classpath I have had to come up with what I consider to be a hacky workaround. I unpack the zip archive to a temp directory then copy one of the resulting directories into the /test-classes folder. I also had to make the clean step delete the temp directory. Here are the relevant parts of the pom:
<groupId>com.my.package</groupId>
<artifactId>test-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>My Test Project</name>
<properties>
<config.artifactId>environment-dev</config.artifactId>
<config.version>2.0.8-SNAPSHOT</config.version>
<tempDir>${project.basedir}/temp</tempDir>
</properties>
<build>
<plugins>
...
<!-- clean out our custom temp directory as well as the default dir during clean phase-->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.3</version>
<configuration>
<filesets>
<fileset>
<directory>${tempDir}</directory>
</fileset>
</filesets>
</configuration>
</plugin>
<!-- since the config dependency is a zip it does not get added to the classpath. So we extract
it to a temp dir, then copy the content we need into a directory on the classpath -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>unpack-config</id>
<phase>compile</phase>
<goals><goal>unpack-dependencies</goal></goals>
<configuration>
<includeGroupIds>com.my.package.config</includeGroupIds>
<includeArtifactIds>${config.artifactId}</includeArtifactIds>
<includeClassifiers>config</includeClassifiers>
<outputDirectory>${tempDir}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!-- copy the content of the zip file that we extracted into a directory on the classpath -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<phase>compile</phase>
<goals><goal>copy-resources</goal></goals>
<configuration>
<outputDirectory>${project.build.directory}/test-classes/TargetDir</outputDirectory>
<resources>
<resource>
<directory>${tempDir}/${config.artifactId}-${config.version}/TargetDir</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
...
<dependency>
<groupId>com.my.package.config</groupId>
<artifactId>${config.artifactId}</artifactId>
<version>${config.version}</version>
<classifier>config</classifier>
<type>zip</type>
</dependency>
</dependencies>
There must be a better way of doing this.
Can I force maven to treat the zip file as if it were a jar? The link I provided has a tantalising hint that this might once have been possible, but I can't find anything relevant in the documentation. This seems like such a simple thing to be able to do, I really hope I've just missed a config parameter somewhere. Can anyone suggest a better way of getting the content of a zip dependency onto the classpath?
I would unzip the dependency into a subdirectory of the target directory and add that directory to the additionalClasspathElements configuration of the surefire plugin.
<properties>
<config.artifactId>environment-dev</config.artifactId>
<config.version>2.0.8-SNAPSHOT</config.version>
<unzipDir>${project.build.directory}/addTestClasspath</unzipDir>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>unpack-config</id>
<phase>compile</phase>
<goals><goal>unpack-dependencies</goal></goals>
<configuration>
<includeGroupIds>com.my.package.config</includeGroupIds>
<includeArtifactIds>${config.artifactId}</includeArtifactIds>
<includeClassifiers>config</includeClassifiers>
<outputDirectory>${unzipDir}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<additionalClasspathElements>
<additionalClasspathElement>${unzipDir}</additionalClasspathElement>
</additionalClasspathElements>
</configuration>
</plugin>
</plugins>
</build>
In this case you can omit the clean plugin config because everything is under the target folder which will be deleted by the clean plugin by default.
Sadly this configuration does only work on the command line and not within eclipse, because the m2e plugin does not honor the additionalClasspathElement. See the jira issue MNGECLIPSE-1213

maven-jar-plugin file filterring

I have a some perl file in my src/main/java/com/pac/report.pl which I want to package as part of my classes in the jar file.
Using maven maven-jar-plugin include directives, I have tried below and various other suggestions I pulled off the web, but doesn't copy the perl file as part of my classes in the jar file. Does anyone know what I am doing wrong.
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<includes>
<include>**/*</include>
</includes>
</configuration>
</plugin>
EDIT
Also let me point out that I don't want to place the file in the resource directory due to legacy call and dependent reasons.
That is because the classes packaged into your jar aren't taken from src, but rather from target (specifically /target/classes), and the compiler completely ignores your non-java file.
Try placing your file in src/main/resources/com/pac/report.pl and it should be packaged into the jar (with the relative path of /com/pac/report.pl) since thats the default location where the resources plugin looks for additional files to add to /target before the jar plugin runs.
EDIT - or, if you dont want to / cant do this the way maven expects, you could manually bind an execution of the resources plugin to the lifecycle to pick up your file and copy it over to target. something like this:
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>compile</phase> <!-- or later -->
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/classes</outputDirectory>
<resources>
<resource>
<!-- path to your *.pl file here -->
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
...
</build>

Categories