Create a simple application with custom configuration files using Maven

I need to create a simple application to configure and launch my client on my site. I use the Spring structure, so I have several configuration files that should be in the class path. I am using Maven2 with Netbeans as my IDE.

I can create and run my application using Netbeans / Maven, and I use the Application Assembler Maven plugin to create an executable application. All this works fine, except that my Spring configuration files should be placed in src / main / resources , which means they will be packed into the resulting JAR file.

I need my client to be able to modify configuration files to test them, but it is not wise to ask them to modify copies that are packaged in a JAR.

There may be a number of solutions, but it seems to me that the easiest way would be to force Maven to not pack the application and configuration files in the JAR at all, just leaving them something like classes from which they can be launched. This will allow the user to easily modify configuration files. Unfortunately, I cannot figure out how to get Maven to "package" the application this way or how to get AppAssembler to generate the resulting runnable.

Here is an excerpt from my pom.xml, which may help illustrate what I'm trying to do:

... <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.1.0.RELEASE</version> </dependency> ... stuff deleted ... </dependencies> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>appassembler-maven-plugin</artifactId> <version>1.2</version> <configuration> <!-- Set the target configuration directory to be used in the bin scripts --> <configurationDirectory>conf</configurationDirectory> <!-- Copy the contents from "/src/main/config" to the target configuration directory in the assembled application --> <copyConfigurationDirectory>true</copyConfigurationDirectory> <!-- Include the target configuration directory in the beginning of the classpath declaration in the bin scripts --> <includeConfigurationDirectoryInClasspath> true </includeConfigurationDirectoryInClasspath> <platforms> <platform>windows</platform> </platforms> <programs> <program> <mainClass>org.my.path.App</mainClass> <name>app</name> </program> </programs> </configuration> </plugin> </plugins> </build> ... 
+4
source share
2 answers

None of the packaged jar files or a set of decompressed class files is a good format for professional customer delivery. Look at such brilliant apache applications like tomcat, ant and maven, they are sent as tar.gz or zip file after downloading, just extract them and you will get a beautiful and clean directory structure:

conf -> put the configuration file e.g. * .properties, logback.xml here
doc -> readme.txt, userguide.doc, etc.
lib → put you core.jar with the javascript dependency file here
run.bat -> run script for Windows
run.sh -> run script for Unix

We can do such things with Maven. Note that you must design and implement your own kernel for reading in order to correctly read * .properties from the conf directory. then use the maven-assembly-plugin application that you use in this classic directory structure.

Example pom.xml for a command line application:

  <!-- Pack executable jar, dependencies and other resource into tar.gz --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.2-beta-5</version> <executions> <execution> <phase>package</phase> <goals><goal>attached</goal></goals> </execution> </executions> <configuration> <descriptors> <descriptor>src/main/assembly/binary-deployment.xml</descriptor> </descriptors> </configuration> </plugin> 

Example binary-deployment.xml for a command line application:

 <!-- release package directory structure: *.tar.gz conf *.xml *.properties lib application jar third party jar dependencies run.sh run.bat --> <assembly> <id>bin</id> <formats> <format>tar.gz</format> </formats> <includeBaseDirectory>true</includeBaseDirectory> <fileSets> <fileSet> <directory>src/main/java</directory> <outputDirectory>conf</outputDirectory> <includes> <include>*.xml</include> <include>*.properties</include> </includes> </fileSet> <fileSet> <directory>src/main/bin</directory> <outputDirectory></outputDirectory> <filtered>true</filtered> <fileMode>755</fileMode> </fileSet> <fileSet> <directory>src/main/doc</directory> <outputDirectory>doc</outputDirectory> <filtered>true</filtered> </fileSet> </fileSets> <dependencySets> <dependencySet> <outputDirectory>lib</outputDirectory> <useProjectArtifact>true</useProjectArtifact> <unpack>false</unpack> <scope>runtime</scope> </dependencySet> </dependencySets> </assembly> 
+5
source

If you are not misleading, I think you want the jar and config to be separated from each other, with a jar set up for client testing. The following can do this for you, using copy-maven-plugin, it can accomplish almost the tasks that the plugin-builder will do, for example: copy, dependency and much more - loading, loading, moving, ....

  <plugin> <groupId>com.github.goldin</groupId> <artifactId>copy-maven-plugin</artifactId> <version>0.2.5</version> <executions> <execution> <id>create-archive</id> <phase>package</phase> <goals> <goal>copy</goal> </goals> </execution> </executions> <configuration> <resources> <!--copy your scripts to ${myOutPutPath}/bin--> <resource> <targetPath>${myOutPutPath}/bin</targetPath> <directory>${project.basedir}/src/main/scripts</directory> <includes> <include>*</include> </includes> </resource> <resource> <!--copy your configs--> <targetPath>${myOutPutPath}/conf</targetPath> <directory>${project.basedir}/src/main/config</directory> <include>*</include> </resource> </resources> </configuration> </plugin> 

Download the main jar and put on your $ {myOutPutPath}

  <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.3.1</version> <!-- The configuration of the plugin --> <configuration> <outputDirectory>${myOutPutPath}</outputDirectory> <!-- Configuration of the archiver --> <archive> <!-- Manifest specific configuration --> <manifest> <!-- Classpath is added to the manifest of the created jar file. --> <addClasspath>true</addClasspath> <!-- Configures the classpath prefix. This configuration option is used to specify that all needed libraries are found under lib/ directory. --> <classpathPrefix>lib/</classpathPrefix> <!-- Specifies the main class of the application --> <mainClass>com.xinguard.snmp.SNMP_ETL</mainClass> </manifest> <!-- you need to add some classpath by yourself, like conf here for client to use--> <manifestEntries> <Class-Path>conf/</Class-Path> </manifestEntries> </archive> </configuration> </plugin> 

then pack the lib jar directory into lib in the jar directory.

  <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy-dependencies</id> <phase>prepare-package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${myOutPutPath}/lib</outputDirectory> </configuration> </execution> </executions> </plugin> 
+4
source

Source: https://habr.com/ru/post/1397962/


All Articles