Maven – How to generate a package with external resources
While making a java batch, it’s a common way to generate a working directory structure like the following:
1 2 3 4 |
bin conf lib logs |
The directories above will contain:
- conf – external configuration properties
- lib – the jar libraries, comprensive of the main jar
- logs – the log files
- bin – the sh / bat script file within the instructions to run the batch properly
Our source structure folder will be similar:
- applconf – external configuration properties
- scripts – the sh / bat script file within the instructions to run the batch properly
- logs – the log files
Now it’s time to edit pom.xml properly.
pom.xml
The following lines will copy applconf content inside target conf directory
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.4.3</version> <execution> <id>copy-applconf-resources</id> <!-- here the phase you need --> <phase>validate</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${basedir}/target/conf</outputDirectory> <resources> <resource> <directory>applconf</directory> <filtering>true</filtering> </resource> </resources> </configuration> </execution> |
The following lines copy scripts file inside bin directory
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<execution> <id>copy-shellScript-resources</id> <!-- here the phase you need --> <phase>validate</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${basedir}/target/bin</outputDirectory> <resources> <resource> <directory>scripts</directory> <filtering>true</filtering> </resource> </resources> </configuration> </execution> |
The following copy main jar with dependencies inside lib directory:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<!-- copy dependencies into lib directory --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.10</version> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>true</overWriteSnapshots> <excludeTransitive>true</excludeTransitive> </configuration> </execution> </executions> </plugin> |
The following will generate manifest.inf within the main jar package
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.2</version> <configuration> <useDefaultManifestFile>false</useDefaultManifestFile> <archive> <index>true</index> <manifest> <addClasspath>false</addClasspath> <addDefaultImplementationEntries>true</addDefaultImplementationEntries> <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries> </manifest> <manifestEntries> <Built-By>${organization.name}</Built-By> <Build-Jdk>${java.version}</Build-Jdk> </manifestEntries> </archive> </configuration> </plugin> |
Finally, the pom.xml declare that directive to build the zipped package are contained inside assembly-app.xml file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.2-beta-4</version> <configuration> <descriptors> <descriptor>assembly-app.xml</descriptor> </descriptors> <finalName>${project.artifactId}-${project.version}</finalName> </configuration> <executions> <execution> <id>make-assembly</id> <!-- this is used for inheritance merges --> <phase>package</phase> <!-- append to the packaging phase. --> <goals> <goal>single</goal> <!-- goals == mojos --> </goals> </execution> </executions> </plugin> |
assembly-app.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<assembly> <id>batch-app-dist</id> <formats> <format>zip</format> <format>tar.gz</format> <format>tar.bz2</format> </formats> <includeBaseDirectory>true</includeBaseDirectory> <fileSets> <fileSet> <directory>applconf</directory> <lineEnding>unix</lineEnding> <outputDirectory>conf</outputDirectory> </fileSet> <fileSet> <fileMode>666</fileMode> <directory>logs</directory> <outputDirectory>logs</outputDirectory> </fileSet> <fileSet> <fileMode>755</fileMode> <directory>shellScript</directory> <lineEnding>unix</lineEnding> <outputDirectory>bin</outputDirectory> </fileSet> </fileSets> <!-- the project dependencies --> <dependencySets> <dependencySet> <outputDirectory>lib</outputDirectory> <unpack>false</unpack> <scope>runtime</scope> </dependencySet> </dependencySets> </assembly> |
These directives will copy jar dependencies inside lib directory and applconf content inside conf directory.
If our need is to place main jar outside lib directory, copy the following lines instead:
1 2 3 4 5 6 7 8 9 |
<fileSets> <fileSet> <directory>${project.build.directory}</directory> <outputDirectory>/</outputDirectory> <includes> <include>*.jar</include> </includes> </fileSet> [...] |
1 2 3 4 5 6 7 8 |
<dependencySets> <dependencySet> <scope>runtime</scope> <outputDirectory>/lib</outputDirectory> <useProjectArtifact>false</useProjectArtifact> <unpack>false</unpack> </dependencySet> </dependencySets> |
Leave a Reply