MAVEN Package Spring Project

Source: Internet
Author: User

The Springframework control is used in the program (mainly to operate the database with mybatis-spring).

Using MAVEN to manage the build of your project, you now need to build a jar package that contains all the dependent jar packages and can be run using Java-jar [filename] at the command line.

1. Maven-assembly-plugin

At the very beginning, try using the Maven-assembly-plugin plugin, which is configured as follows:

<build> <plugins> <plugin> <groupid>org.apache.maven.plugins</ Groupid> <artifactId>maven-assembly-plugin</artifactId> <version>2.4</version> <configuration> <descriptorRefs> &lt                    ;d escriptorref>jar-with-dependencies</descriptorref> </descriptorRefs> <archive> <manifest> &LT;MAINCLASS&GT;MAIN&LT;/MAINCLASS&G                        T </manifest> </archive> </configuration> <executions&                    Gt <execution> <id>make-assembly</id> <phase>package&lt                        ;/phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin&gt        ; </plugins> </build>

However, after the command line was packaged with the MVN Package command, after running Java-jar, the error is as follows:

 for namespace [http://Www.springframework.org/schema/context] class path resource [Javaprojectapplicationcontext.xml]

Google for a while, some people on the internet said this is a bug Assembly-plugin plugin, because it in the third party packaging is, for Meta-inf under the Spring.handlers,spring.schemas and so on several files with the same name covered.

It is recommended to use the Maven-shade-plugin plugin.

2.maven-shade-plugin Plug-in

The shade plugin can import all dependent packages into the final executable jar package. Depending on the content on the Web, the POM is set up as follows:

<build> <plugins> <plugin> <groupid>org.apache.maven.plugins</groupi D> <artifactId>maven-shade-plugin</artifactId> <version>1.4</version> <executions> <execution> <phase>pack                      Age</phase> <goals> <goal>shade</goal>                              </goals> <configuration> <transformers> <Transformer Implementation="Org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.defonds.RsaEncryptor</mainClass> </transformer> <Transformer Implementation="Org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.handlers</resource> </transformer> <Transformer Implementation="Org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.schemas</resource>                  </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build&gt ;

However, after the implementation of the error, as follows:

inch " Main "  for Manifest Main attributes

After the lookup, the reason is that some of the packages are repeated references, and after packing some more *. SF and other files, resulting in an error.

Solution, add the following code to the Pom.xml file:

<configuration>    <filters>      <filter>        <artifact>*:* </artifact>        < excludes>          <exclude>meta-inf/*. Sf</exclude>          <exclude>meta-inf/*. Dsa</exclude>          <exclude>meta-inf/*. rsa</exclude>        </excludes>      </filter>    </filters>  </configuration >  

MVN CLEAN;MVN package;  Java-jar ... After that, everything works fine.

Tossing a lot of day, and finally moving forward asynchronously, mark.

MAVEN Package Spring Project

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.