For the first time in the blog Park to write a blog, write bad people forgive me ah!!
first, before you pack with Maven, be sure to confirm the JDK version and MAVEN version you're using. The best is to use jdk1.8 and maven3.3 above, the version is too low to be prone to some unexpected problems (now should be used to compare the new version, the old version is also useful, if you use the lower version, do not worry, the following will have a solution).
After confirming the good version, it depends on whether your JDK is installed, the environment variables are not configured (Maven also checks), here is the installation and configuration of JDK and maven:
- Here is the installation and configuration of the JDK: jingyan.baidu.com/article/6dad5075d1dc40a123e36ea3.html
- Here is Maven's installation and configuration, very detailed: jingyan.baidu.com/article/6c67b1d646ae842786bb1e7a.html
Second, the JDK and MAVEN are configured, it is necessary to look at the local MAVEN library (this step should be done in configuration maven, in the MAVEN settings file to configure the dependent download path, in my file has settings configuration, please download it yourself).
Third, before packaging also check the project's Pom file is not configured
⑴, Configure the following code in the Pom file that needs to be packaged:
<?xml version= "1.0" encoding= "UTF-8"? ><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> Project name </groupId> <artifactId> package name of packaged Pom </artifactId> <ver Sion>1.0.0</version> <packaging>pom</packaging> <name> Package name of the packaged pom </name> <url >http://maven.apache.org</url><modules> <module> Name of each sub-package </module>There are several sub-packages to write several module</modules> <properties> <PROJECT.BUILD.SOURCEENCODING>UTF-8</PROJECT.BUILD.SOURCEENCODING&G T <junit.version>4.10</junit.version> <mysql.driver>com.mysql.jdbc.Driver</mysql.driver> <mysql.url>jdbc:mysql://localhost:3306/mysql</mysql.url> <mysql.username> database user name </mysql.username> <mysql.password> database Password </mysql.password> <spring.version>3.2.4.RELEASE</spring.version> </properties> <!--global dependent-<depende ncies> <dependency> <groupId>junit</groupId> <artifactId>junit< /artifactid> <version>${junit.version}</version> <!--<scope>test</scope& Gt -</dependency> </dependencies> <distributionManagement> </distributionmanagemen T> <build> <finalName> Here's the name of the war package after packaging </finalName> <pluginManagement> <plugins> <plugin> <artifactid>maven-compiler-plugin</artifactid& Gt <version>2.3.2</version> <configuration> <source>1.7≪/source> <target>1.7</target> <verbose> true</verbose> <fork>true</fork> <executable>${JAVA_HOME}\bin\javac</executable> </c onfiguration> </plugin> <plugin> <artifactid>maven-s Urefire-plugin</artifactid> <version>2.12.4</version> <configur Ation> <skip>true</skip> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactid>maven-war-plugi N</artifactid> <version>2.1.1</version> <configuration> <webResources> <resource><!--Package lib folder jar-- <directory>${project.basedir}/lib</directory> <targetpat H>web-inf/lib</targetpath> <filtering>false</filtering> <includes> <include>**/*.jar</include> </includes> </resource> </webResources> <archive> <ADDMAVENDESCRI Ptor>false</addmavendescriptor> </archive> </configuration> </plugin> <!--<plugin> <ARTIFACTID>MAVEN-ANTRUN-PL Ugin</artifactid> <executions> <execution> <id>copy-war</id> <phase>package</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks>copy War package to local Tomcat's WebApp directory <echo message= "Copy War file to Tomcat WebApps ...."/> <copy file= "ta Rget/xxx.war "todir=" D:/work/apache-tomcat-8.0.1/webapps "overwrite=" true "/> Copy config file to local tomcat's conf directory <echo message= "Copy confi g file to AppConfig .... "/> <copy todir=" D:/work/apache-tomcat-8.0.1/appconfig " Preservelastmodified= "true" overwrite= "true" > <fileset dir= "src/main/resources/conf" > <include name= "*.propertie S "/> </fileset> </copy> </tasks> </configuration> </execut Ion> </executions> </plugin> <plugin> <groupid& Gt;org.apache.maven.plugins</groupid> <artifactId>maven-source-plugin</artifactId> <version>2.1.2</version> <executions> <exe Cution> <id>attach-sources</id> <goals> <goal>jar-no-fork</goal> </goals> </execution> </executions> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> < Artifactid>maven-javaDoc-plugin</artifactid> <version>2.9</version> <executions> <execution> <id>attach-javadocs</id> <goals> <goal>jar</goal> </goals& Gt </execution> </executions> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </pluginManagement> </build>
⑵, Add the following configuration to the POM for the child package to hold the War package:
<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> <parent> <groupId> Store packages for all sub-packages </groupId> <artifactId> package names packaged with Pom & Lt;/artifactid> <version>1.0.0</version> <relativepath> ... /pom.xml</relativepath> </parent> <artifactId> Store packages for all child packages (the name of the child package that holds the war package) </artifactId> < Packaging>war</packaging> <build> <sourceDirectory>src/main/java</sourceDirectory> <testSourceDirectory>src/test/java</testSourceDirectory> <resources> <RESOURC E> <directory>src/main/resources</directory> <excludes> <exclude>**/*.java</exclude> </excludes> </resource> </resources> <p Lugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.7</sou Rce> <target>1.7</target> <verbose>true</verbose> <fork>true</fork> <executable>${JAVA_HOME}\bin\javac</executable> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifa Ctid>maven-war-plugin</artifactid> <version>2.1.1</version> <configu Ration> <webResources> <resource> <directory>src/main/we app</directory> </resource> </webResources> < ;/configuration> </plugin> </plugins> </build>
⑶, Add the following configuration to the other child packages:
<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/maven-v4_0_0.xsd" > <parent> <groupId> Save Put packages for all child packages </groupId> <artifactId> packages packaged with Pom </artifactId> <version>1.0.0</version> <relativepath>, .... /pom.xml</relativepath> </parent> <modelVersion>4.0.0</modelVersion> <artifactId> Child package name </artifactId> <build> <plugins> <plugin> <artifa Ctid>maven-jar-plugin</artifactid> <version>2.3.2</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>2.1.2</version> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar-no-fork</goal> </goal s> </execution> </executions> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build>
Four, the above are checked no problem, you can open the Terminal or DOS window to use the MAVEN command to pack and write the project! Linux system directly right mouse click Open Terminal (used Linux should know O (* ̄︶ ̄*) o), Windows system Press Win+r (win is the CTRL key and ALT key in the middle of the field grid icon) after the input cmd into the command line editing mode, Or I recommend that you use Windows PowerShell ISE that comes with Windows, search for PowerShell from the Start menu bar or Cortana, click here, and there are various Windows commands.
Then use the command to go to the same directory as the Pom file for the item you want to package, and use the MVN package or MVN clean packages for packaging.
If you are packing with an exception that says ClassNotFound, it should be that the package is not loaded, and you need to look at the message that follows, and it will tell you which jar is missing, just add the missing jar to your local maven library or maven.
If the exception is similar to the one shown, follow the list below to find the maven-(plug-in name)-plugin (the plug-in name is the resources, jar, surefire, etc.) to modify (pro-test valid):
- The exception to Maven-surefire-plugin (default test) is that MAVEN's surefire plugin version is too high to adjust version to 2.12.4.
- The maven-compiler-plugin exception, which is also caused by the plug-in version, adjusts version to 2.3.2.
- Maven-war-plugin exception, change version to 2.1.1.
- Maven-jar-plugin exception, change version to 2.3.2.
- Maven-source-plugin exception, change version to 2.1.2.
If it is not the above problem, garbled or prompted to say in which class how many lines have errors, there should be some code, such as: Try ... catch {} statement, it is known that after jdk1.7, the IO stream object can be written in parentheses behind the try, such as
New File (FilePath); Try New FileInputStream (inFile); New tararchiveinputstream (FIS);) { ...... Catch (Exception e) { e.printstacktrace (); }
If such an exception occurs, you need to include the following code in the Pom file:
<pluginManagement> <plugins> <plugin> <artifactid>maven-compiler-plugin </artifactId> <version>2.3.2</version> <configuration> <source>1.7 </source> <target>1.7</target> <verbose>true</verbose> < Fork>true</fork> <executable>${JAVA_HOME}\bin\javac</executable> </configuration> </plugin> </plugins></pluginManagement>
${java_home} Here you need to write your JAVA installation path, for example my is D:\Java\jdk1.8, so <executable>d:\java\jdk1.8\bin\javac</executable >
at this point, the use of MAVEN commands to package the war project and solve the problem is complete! If the readers have other questions, please give me a message or a private messages! Blog for the first time, I hope you support, thank you thanks♪ (・ω・) Dentetsu