最近項目用到maven,完成後要將項目打包成可執行的jar,在網上搜了很多打包方法,但總有個個別問題在那裏
pom配置文件增加下面配置,執行mvn clean package
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</build>
1.打包時提示軟件包 org.springframework.context 不存在
因爲引用的spring的jar是通過其他的項目引用進來的,所以無法找到。將jar加入引用解決問題
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
2.依賴的jar沒有引用無法直接使用 ,pom中再增加對應的配置,通過${project.build.directory}/lib將引用的jar打包到項目下的lib文件中
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
3.這個時候同java -jar 打包好的jar發現還是找不到jar包
把打包到lib下的jar包copy到打包好的jar包同級目錄下,重新運行,成功。
注意:要看jar的META-INF/MANIFEST.MF文件裏面,一定要有Class-Path和Main-Class,指向對應的文件
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: zml
Build-Jdk: 1.6.0_24
Main-Class: Main
Class-Path: spring-jdbc-3.1.1.RELEASE.jar spring-beans-3.1.1.RELEASE.j
ar spring-core-3.1.1.RELEASE.jar spring-asm-3.1.1.RELEASE.jar commons
-logging-1.1.1.jar spring-tx-3.1.1.RELEASE.jar aopalliance-1.0.jar sp
ring-aop-3.1.1.RELEASE.jar spring-context-3.1.1.RELEASE.jar spring-ex
pression-3.1.1.RELEASE.jar jackson-mapper-asl-1.9.12.jar jackson-core
-asl-1.9.12.jar db2jcc-1.0.jar commons-dbcp-1.4.jar commons-pool-1.5.4.jar<span style="font-family: Arial, Helvetica, sans-serif;"> slf4j-api-1.7.7.jar slf4j-log4j12-1.</span><span style="font-family: Arial, Helvetica, sans-serif;"> 7.7.jar log4j-1.2.17.jar</span>