Configure the Pom.xml method of using MAVEN to package Java Engineering (recommended) _java

Source: Internet
Author: User

Recently, because of the project needs, studied the MAVEN package, project to do

1, generate 3 directory/lib,/conf,/bin directory

2, compile and copy all jar directories to the/lib directory (including the jars in Maven's jar and Lib directories, and the compiled jar packages)

3, copy all startup scripts from the engineering root directory to the/bin directory

4, copy all the configuration files from Src/main/resources to/conf

Here is the configuration of the pom.xml, I put the relevant configuration are annotated, a look can be understood, the build node to copy to your project, you can basically use:

<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.sour Ceencoding>utf-8</project.build.sourceencoding> </properties> <dependencies> <!--here omit N line-- > </dependencies> <build> <sourceDirectory>src/main/java</sourceDirectory> <re sources> <!--copy all files in the Src/main/resources directory to the Conf directory--> <resource> &LT;DIRECTORY&GT;SRC /main/resources</directory> <targetpatH>${project.build.directory}/conf</targetpath> </resource> <!--copy all the files in the Lib directory to the Lib directory (There may be some jar packs that are not found in Maven and need to be placed in the Lib directory)--> <resource> <directory>lib</directory> &L T;targetpath>${project.build.directory}/lib</targetpath> </resource> <!--The script file placed in the root directory. sh,.b
          At copy to bin directory--> <resource> <directory>.</directory> <includes>
        <include>**/*.sh</include> <include>**/*.bat</include> </includes> <targetPath>${project.build.directory}/bin</targetPath> </resource> </resources> & lt;plugins> <!--plugin--> <plugin> <groupid>org.apache.maven.plugins</g for compilation
        Roupid> <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 not be matched--> <executable>c:\program Files (x86) \java\jdk1.8.0_91 \bin\javac.exe</executable> </configuration> </plugin> <!--Plugi for generating jar packages n--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactid>ma ven-jar-plugin</artifactid> <version>2.6</version> <configuration> <! --Put the generated jar pack under the Lib directory (along with all other jar packs)--> <outputdirectory>${project.build.directory}/lib</outputdirector
              y> <archive> <manifest> <addClasspath>true</addClasspath> <classpathprefix>lib/</classpathprefix> </manifest> </archive> &LT;EXCLUDES&G
          T <!--exclude some files, do not put in the jar package, here is to eliminate the files in the src/main/resources (they should be placed in the Conf directory) here can only specify the target file to exclude, but not to specify the source file, although not perfect , but the basic can 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-dependenc y-plugin</artifactid> <version>2.10</version> <executions> <execution > <id>copy-dependencies</id> <phase>package</phase> <goa ls> <goal>copy-dependencies</goal> </goals> <configuration&Gt <!--copy all of the Maven jar packages that are dependent to the Lib directory (so that all jar packages are in the Lib directory)--> <outputdirectory>${project.build.directory
      }/lib</outputdirectory> </configuration> </execution> </executions> </plugin> <!--for copying resource plugin--> <plugin> <groupid>org.apac He.maven.plugins</groupid> <artifactId>maven-resources-plugin</artifactId> &LT;VERSION&G t;2.7</version> <configuration> <encoding>UTF-8</encoding> </configur Ation> </plugin> <!--Configure plugin--> <plugin> <artifactid>mav to generate source code jars En-source-plugin</artifactid> <version>2.4</version> <configuration> ;attach>true</attach> <encoding>UTF-8</encoding> <!--Configure the storage path for source code jar files, and other jar files Together in the Lib eyeRecord--> <outputDirectory>${project.build.directory}/lib</outputDirectory> </configuratio n> <executions> <execution> <phase>compile</phase> &L t;goals> <goal>jar</goal> </goals> </execution> ;/executions> </plugin> </plugins> </build> </project>

Then perform the MAVEN package package. If it is eclipse, enter package in the goals of project Explorer-> run as–> Maven build...–>, and click Run to compile.

The startup script in the bin directory Start.bat can write this:

@ECHO off
set classpath=%java_home%/lib set
Java=%java_home%/bin/java

set classpath=%classpath%;. /conf
set java_options=-djava.ext.dirs= ". /lib "

"%java% "-xms512m-xmx1024m-classpath"%classpath% "%java_options% test. Hangqingentrance

Above:

The Conf directory is added to the classpath because the Conf directory holds a copy of the configuration file from the src/main/resources.

Set -djava.ext.dirs= "... /lib ". because jar packages are placed in the Lib directory.

The above is a small set for everyone to configure Pom.xml with Maven package Java Engineering Method (recommended) All content, I hope that we support cloud Habitat Community ~

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.