Transferred from: http://blog.csdn.net/johnnywww/article/details/7964326
1. Modify Pom.xml Add the following content
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId> maven-jar-plugin</artifactid> <version>2.4</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> < Classpathprefix>lib/</classpathprefix> <mainClass>com.sysware.HelloWorld</mainClass> </manifest> </archive> </configuration> </plugin>
Run the MVN clean package to
2. Add the following in Pom.xml
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactid>mav En-assembly-plugin</artifactid> <version>2.3</version> <configuration> <appendAssemblyId>false</appendAssemblyId> <descriptorRefs> <descriptorref>jar-with-d Ependencies</descriptorref> </descriptorRefs> <archive> <manifest> <mainclass>com.juvenxu.mvnbook.helloworld.helloworld</ma inclass> </manifest> </archive> </configuration > <executions> <execution> <id>make-assembly </id> <phase>package</phase> <goals> <goal>assembly</goal> </goals> </execution> </executions> </plugin>
Run MVN assembly:assembly
3.
<build> <finalName>...</finalName> <sourcedirectory>src/main/java</sourcedirector Y> <resources> < Copy!--control resource Files--<resource> <director Y>src/main/resources</directory> <targetPath>${project.build.directory}</targetPath> </resource> </resources> <plugins> <!--set source file encoding-- <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactid>m Aven-compiler-plugin</artifactid> <configuration> <defaultlibbundledir> ;lib</defaultlibbundledir> <source>1.6</source> <target>1.6</target> <encoding>utf-8</encoding> </configuration> </plugin> <!--packaging jar files, configure manifest , add the Lib package to the jar dependency-<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainclass> Monitormain</mainclass> </manifest> </archive> </configuration> </plugin> <!--copy-dependent jar packages to the Lib directory--<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactid>maven-dependency-plugi n</artifactid> <executions> <execution> <id& Gt;copy</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/Lib</outputDirectory> </configuration> </execution> </executions> </plugin> <!--troubleshoot encoding problems with resource files--<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactid>maven-resources-plugin</ Artifactid> <version>2.3</version> <configuration> <encoding>utf-8</encoding> </configuration> </plugin> <!--packaging the source file as a jar file--& Gt <plugin> <artifactId>maven-source-plugin</artifactId> <version>2.1</version> <configuration> <attach>true</attach> <encoding>utf-8</encoding> </configuration> <executions> <execution > <phase>compile</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
4.
<build> <resources> <resource> <targetpath>${project.build.direct Ory}/classes</targetpath> <directory>src/main/resources</directory> <fi Ltering>true</filtering> <includes> <include>**/*.xml</include> </includes> </resource> </resources> <PL Ugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.0</version> <configuration> <source>1.6</source> <target>1.6</targe t> <encoding>UTF-8</encoding> </configuration> </plug in> <plugin> <groupId>org.apache.maven.plugins</groupId> < ;artifactid>maven-shade-plugin</artifactid> <version>2.0</version> < Executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> &L T;configuration> <transformers> <transformer implementation= "Org.apache.maven.plugins.shade.resource.ManifestResourceTransformer" > <mainClass>com.test.testguava.app.App</mainClass> </transformer> <transformer implementation= "or G.apache.maven.plugins.shade.resource.appendingtransformer "> <resource>applica Tioncontext.xml</resource> </transformer> </trans Formers> <shadedArtifactAttached>true</shadedArtifactAttached> <shadedclasSifiername>executable</shadedclassifiername> </configuration> < ;/execution> </executions> </plugin> </plugins> </build>
Maven Package Executable Jar method