Recently, due to project needs, I have studied the packaging of maven, and the project needs to be done.
1. Generate 3 directories /lib, /conf, /bin directories
2. Compile and copy all jar directories to /lib directory (including maven's jar package and jar in the lib directory, as well as compiled jar packages)
3. Copy all the startup scripts from the project root directory to the /bin directory
4. Copy all configuration files from src/main/resources to /conf
The following is the configured pom.xml. I commented on all the relevant configurations and you can understand it at a glance. If you copy the build node to your project, you can basically use it :)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>test</groupId> <artifactId>test.common</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>test.common</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- n lines omitted here--> </dependencies> <build> <sourceDirectory>src/main/java</sourceDirectory> <resources> <!-- Copy all files in the src/main/resources directory to the conf directory--> <resource> <directory>src/main/resources</directory> <targetPath>${project.build.directory}/conf</targetPath> </resource> <!-- Copy all the files in the lib directory into the lib directory (some jar packages may not be found in maven and need to be placed in the lib directory) --> <resource> <directory>lib</directory> <targetPath>${project.build.directory}/lib</targetPath> </resource> <!-- Copy the script file .sh,.bat in the root directory to the bin directory --> <resource> <directory>.</directory> <includes> <include>**/*.sh</include> <include>**/*.bat</include> </includes> <targetPath>${project.build.directory}/bin</targetPath> </resource> </resources> <plugins> <!-- plugin for compilation --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <fork>true</fork> <defaultLibBundleDir>lib</defaultLibBundleDir> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> <!-- If JAVA_HOME is configured, the following should be free of use --> <executable>C:/Program Files (x86)/Java/jdk1.8.0_91/bin/javac.exe</executable> </configuration> </plugin> <!-- plugin for generating jar packages --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.6</version> <configuration> <!-- Put the generated jar package in the lib directory (with all other jar packages) --> <outputDirectory>${project.build.directory}/lib</outputDirectory> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> </manifest> </archive> <excludes> <!-- Exclude some files and do not put them in the jar package. This is to exclude files in src/main/resources (they should be placed in the conf directory). Here you can only specify the target files to be excluded, but not the source files. Although they are not perfect, they can basically achieve the goal. --> <exclude>*.xml</exclude> <exclude>*.properties</exclude> </excludes> </configuration> </plugin> <!-- Plugin for copying maven dependencies --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependent-plugin</artifactId> <version>2.10</version> <executions> <execution> <id>copy-dependents</id> <phase>package</phase> <goals> <goal>copy-dependents</goal> </goals> <configuration> <!-- Copy all maven jar packages that depend on to the lib directory (so that all jar packages are in the lib directory) --> <outputDirectory>${project.build.directory}/lib</outputDirectory> </configuration> </execution> </executions> </plugin> <!-- plugin for copying resource --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.7</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- Configure the plugin that generates the source code jar --> <plugin> <artifactId>maven-source-plugin</artifactId> <version>2.4</version> <configuration> <attach>true</attach> <encoding>UTF-8</encoding> <!-- Configure the storage path of the source code jar file and place it in the lib directory with other jar files --> <outputDirectory>${project.build.directory}/lib</outputDirectory> </configuration> <executions> <execution> <phase>compile</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> </plugins> </build></project>Then execute maven package packaging. If it is eclipse, enter package in Project Explorer->Run As>Maven Build…>Goals and click Run to compile.
The startup script start.bat in the bin directory can be written like this:
@ECHO OFFset CLASSPATH=%JAVA_HOME%/libset JAVA=%JAVA_HOME%/bin/javaset CLASSPATH=%CLASSPATH%;../confset JAVA_OPTIONS=-Djava.ext.dirs="../lib""%JAVA%" -Xms512m -Xmx1024m -classpath "%CLASSPATH%" %JAVA_OPTIONS% test.HangqingEntrance
above:
Add the conf directory to CLASSPATH because the conf directory holds configuration files copied from src/main/resources.
Settings -Djava.ext.dirs="../lib". Because jar packages are placed in the lib directory.
The above is the full content of the method (recommended) for configuring pom.xml to use maven to package java projects. I hope everyone can support Wulin.com more~