Creating directory structure for application with maven - maven

How can I tell maven to create a particular directory structure and to put jar files in certain places?
For example I want to put put some jars in a directory called plugins which are loaded at runtime.
Is that possible? Or am I missing the principles behind maven?

It looks like your jars is your project dependency, so to copy all dependencies at plugins folder use next:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-alldeps</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/plugins</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<excludeTransitive>false</excludeTransitive>
</configuration>
</execution>
</executions>
</plugin>
When your project use jars like resources, use maven resource section or next plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-config-files</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/plugins/</outputDirectory>
<resources>
<resource>
<filtering>false</filtering>
<directory>${project.basedir}/jars/</directory>
<includes>
<include>file1.jar</include>
<include>file5.jar</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

Related

How to skip version in jar generated by maven-jar-plugin and clasifier

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>client</id>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>client</classifier>
<includes>
<include>**/com.service/**</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
I want the version to be appended to end of the jar.Right now it is building jar like ship-service-0.1-client.jar,I want it like ship-service-client-0.1.jar and i need to add it as dependency in another project.
You can use
<build>
<finalName>FinalNameOfYourProyect</finalName>
</build>
check this related question

reading properties file into maven pom.xml not working

I need to use values from properties file in maven pom.xml, so i used properties-maven-plugin to read my properties file as follows
pom.xml
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>${basedir}/src/main/resources/qura.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
qura.properties file contains something like this..
config.file.path = resources/python/config/test.py
I need use this config.file.path variable in resource element of pom.xml
pom.xml
<resources>
<resource>
<directory>${basedir}/multilang/</directory>
<includes>
<include>${config.file.path}</include>
</includes>
</resource>
<resources>
But the value for ${config.file.path} is not taking up from qura.properties file and I couldn't find test.py file in jar.
what I'm doing wrong in this code?
Thanks in Advance
try using version 1.0.0 and removing spaces around equal sign in properties file.
eg:
key=value
IMO, it does not matter whether you put the spaces or not around equal sign in properties file. You may need to check if the ${config.file.path} exists in the directory specified by ${basedir}/multilang
The below snippet works for me.
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/main</directory>
<includes>
<include>${config.file.path}</include>
</includes>
</resource>
</resources>
</configuration>
<inherited></inherited>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>${basedir}/src/main/resources/qura.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
</plugins>

Move a config file into etc folder(of Karaf) when a (Maven)bundle is deployed

I want to move a cfg file into the etc folder of karaf whenever a bundle is deployed.
the cfg file is in under src/main/resource .i tried the following in the pom but its not working.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<phase>deploy</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>Using env.test.properties</echo>
<copy file="src/main/resources/test.cfg" tofile="${env.KARAF_HOME}/etc/test.cfg"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
How can i do it ?
One of the solution could be:
- put your test.cfg file in a more specific folder. (eg: src/main/resources/cfg)
- use the maven resources plugin
This is a working example based on the maven phase generate-resources (replace that phase by deploy in your case):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-to-karaf</id>
<phase>generate-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/main/resources/cfg</directory>
<filtering>true</filtering>
</resource>
</resources>
<outputDirectory>D:\apache-karaf-3.0.1\etc\</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

Build multiple artifacts with different classifiers at once

W want my maven project to produce three artifacts with different classifiers at once. I know that I can produce it with modules etc. This is actually a resources project that I want to produce configuration for DEV, STAGE and PROD environment.
What I want to have is to run mvn:install once and have my.group:resources:1.0:dev, my.group:resources:1.0:stage and my.group:resources:1.0:prod in my repo.
This can be done without profiles if you specify multiple plugin executions and resource filtering.
Create a properties file for each version in ${basedir}/src/main/filters (e.g. prod.properties, dev.properties) holding appropriate values for each environment.
Turn on filtering for your resources:
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
Now add the resource plugin executions. Note the different filter file and output directory.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>default-resources</id>
<phase>process-resources</phase>
<goals>
<goal>resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.outputDirectory}/dev</outputDirectory>
<filters>
<filter>${basedir}/src/main/filters/dev.properties</filter>
</filters>
</configuration>
</execution>
<execution>
<id>prod</id>
<phase>process-resources</phase>
<goals>
<goal>resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.outputDirectory}/prod</outputDirectory>
<filters>
<filter>${basedir}/src/main/filters/prod.properties</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
Finally, the jar plugin; note classifier and input directory:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>default-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>dev</classifier>
<classesDirectory>${project.build.outputDirectory}/dev</classesDirectory>
</configuration>
</execution>
<execution>
<id>jar-prod</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>prod</classifier>
<classesDirectory>${project.build.outputDirectory}/prod</classesDirectory>
</configuration>
</execution>
</executions>
</plugin>
Running mvn clean install should produce the properly filtered resources in artifacts with dev and prod classifiers like you want.
In the example, I used execution IDs of default-resources and default-jar for the dev versions. Without this you would also get an unclassified jar artifact when you build.
Just an FYI - put the version number in there to make sure you have the version supporting custom filters. In maven 3 I set mine up like this for example. Without version it didn't work.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
...
</plugin>

maven run test for each classifier

I have a maven war project that configure to build for multiple envs using multiple classifier. Here is how i configure the plugin for dev and test envs:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<executions>
<execution>
<id>package-dev</id>
<phase>package</phase>
<configuration>
<classifier>dev</classifier> <webappDirectory>${project.build.directory}/${project.build.finalName}-dev</webappDirectory>
<webResources>
<resource>
<directory>src/env/dev</directory>
</resource>
</webResources>
</configuration>
<goals>
<goal>war</goal>
</goals>
</execution>
<execution>
<id>package-test</id>
<phase>package</phase>
<configuration>
<classifier>test</classifier> <webappDirectory>${project.build.directory}/${project.build.finalName}-test</webappDirectory>
<webResources>
<resource>
<directory>src/env/test</directory>
</resource>
</webResources>
</configuration>
<goals>
<goal>war</goal>
</goals>
</execution>
</executions>
However, I want my test to execute on each of the classifier based on the property files that are overridden for each classifier to make sure all the test will pass in each environment. How can I do that?
Thanks,
Sean

Resources