Maven - Bin (Bash and Bat file creation)

Card Puncher Data Processing

About

How to create standard bash and/or dos file to start a Java application.

Methods

Bash and bat file as a resource

With resource filtering, you can create a bash/dos script template.

The steps are:

  • defining the property value that will be filtered in a property file.
  • creating the bash and dos template
  • adding a copy-resources execution step in the POM

Properties definition

# The property to create a bat and sh file
# main.FileName is the name of the bat and sh file
# main.Class is the name of the main class. It will be replaced in the template
Main.FileName=fileName
Main.Class=mypackage.MyMainClass

Template

  • the bash template
#!/usr/bin/env bash

SCRIPT_PATH=$( cd $(dirname $0) ; pwd -P )

# The cypath transformation is needed if we are on Windows
if [ -e "/usr/bin/cygpath" ]
then
  LIB_PATH="$(cygpath -w $SCRIPT_PATH/lib/)*"
else
  LIB_PATH="$SCRIPT_PATH/lib/*"
fi

# ${1+"$@"} is mandatory if we want to preserve the quotation of the arguments
# otherwise every space will split an argument in two
# See https://stackoverflow.com/questions/743454/space-in-java-command-line-arguments
$SCRIPT_PATH/jre/bin/java -classpath "$LIB_PATH" ${Main.Class} ${1+"$@"}
  • the bat template
@echo off

@REM The script path to reference the included JRE java file
SET SCRIPT_PATH=%~dp0

:APPEND_CLASSPATH
SET CLASSPATH="%SCRIPT_PATH%\lib\*"

%SCRIPT_PATH%\jre\bin\java -classpath %CLASSPATH% ${Main.Class} %*

copy-resources Plugin definition

In the Pom.xml, we add a copy-resources goal with the following option:

  • fileNameFiltering is true: to be able to have another file for each main class.
  • filter containing the path of the property file
  • filtering is true, to replace the property in the the bash and bat template files.
  • resource > directory containing the path of the bash and bat template files.
<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-resources-plugin</artifactId>
	<!-- Creation of the bin file (bat and sh) -->
	<!-- A filter property file must be created in src/build/filters and given for each execution -->
	<executions>
		<execution>
			<!-- Create the bin for the XML Update Class -->
			<id>createXmlUpdateBin</id>
			<phase>compile</phase>
			<goals>
				<goal>copy-resources</goal>
			</goals>
			<configuration>
				<outputDirectory>${project.build.directory}/bin</outputDirectory>
				<overwrite>true</overwrite>
				<fileNameFiltering>true</fileNameFiltering>
				<useBuildFilters>false</useBuildFilters>
				<filters>
					<filter>${project.basedir}/pathToFilterProperties/myMain.properties</filter>
				</filters>
				<resources>
					<resource>
						<filtering>true</filtering>
						<directory>${project.basedir}/pathToTemplate</directory>
						<includes>
							<include>*.*</include>
						</includes>
					</resource>
				</resources>
			</configuration>
		</execution>
	<executions>
</plugin>

appassembler-maven-plugin

See:







Share this page:
Follow us:
Task Runner