How to configure pom. xml to package java project with maven, pom. xmlmaven

Source: Internet
Author: User

How to configure pom. xml to package java project with maven, pom. xmlmaven

 

Recently, due to project requirements, I have studied how to package maven.

1. generate three directories/lib,/conf, And/bin.

2. Compile and copy all the jar directories to the/lib directory (including the jar packages under maven, lib, and 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.

 

Below is the configured pom. xml. I have added comments to all the related configurations. At first glance, I can understand that the build node is copied to your project, and it can be basically used :)

<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: // ma Ven.apache.org </url> <properties> <project. build. sourceEncoding> UTF-8 </project. build. sourceEncoding> </properties> <dependencies> <! -- N rows are omitted here --> </dependencies> <build> <sourceDirectory> src/main/java </sourceDirectory> <resources> <! -- Copy all files under the src/main/resources directory to the conf directory --> <resource> <directory> src/main/resources </directory> <targetPath >$ {project. build. directory}/conf </targetPath> </resource> <! -- Copy all files under the lib directory to 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> <! -- Put the script file in the root directory. sh ,. copy bat 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> <defalilibbundledir> lib </defaultLibBundleDir> <source> 1.7 </source> <target> 1.7 </target> <encoding> UTF-8 </encoding> <! -- If JAVA_HOME is configured, you do not need to configure it. --> <executable> C: \ Program Files (x86) \ Java \ jdk1.8.0 _ 91 \ bin \ javac.exe </executable> </configuration> </plugin> <! -- Plugin used to generate the jar package --> <plugin> <groupId> org. apache. maven. plugins </groupId> <artifactId> maven-jar-plugin </artifactId> <version> 2.6 </version> <configuration> <! -- Place the generated jar package in the lib directory (together 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 the files in src/main/resources (they should be placed in the conf directory). Only the target files to be excluded can be specified here, the source file cannot be specified. Although it is not perfect, it can basically achieve the goal. --> <Exclude> *. xml </exclude> <exclude> *. properties </exclude> </excludes> </configuration> </plugin> <! -- Used to copy the plug-in of maven dependency --> <plugin> <groupId> org. apache. maven. plug-ins </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> <! -- Copy all dependent maven jar packages 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 used to copy the 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 plugin to generate the source code jar --> <plugin> <artifactId> maven-source-plugin </artifactId> <version> 2.4 </version> <configuration> <attach> true </attach> <encoding> UTF-8 </encoding> <! -- Configure the path for storing the source code jar file and put it in the lib directory together 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 run the maven package. For eclipse, go to Project Explorer> Run As> Maven Build... -> Enter the package in Goals and click Run to compile the package.

 

The Startup Script start. bat in the bin directory can be written as follows:

@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 stores the configuration files copied from src/main/resources.

Set-Djava. ext. dirs = "../lib ".Because all jar packages are placed in the lib directory.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.