在pom的plugins中配置
[html] view plain copy- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <configuration>
- <defaultLibBundleDir>lib</defaultLibBundleDir>
- <source>1.6</source>
- <target>1.6</target>
- <encoding>UTF-8</encoding>
- </configuration>
- </plugin>
- <!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 -->
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-jar-plugin</artifactId>
- <configuration>
- <archive>
- <manifest>
- <addClasspath>true</addClasspath>
- <classpathPrefix>lib/</classpathPrefix>
- <mainClass>主类的完全路径名</mainClass>
- </manifest>
- </archive>
- </configuration>
- </plugin>
- <!-- 拷贝依赖的jar包到lib目录 -->
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-dependency-plugin</artifactId>
- <executions>
- <execution>
- <id>copy</id>
- <phase>package</phase>
- <goals>
- <goal>copy-dependencies</goal>
- </goals>
- <configuration>
- <outputDirectory>
- ${project.build.directory}/lib
- </outputDirectory>
- </configuration>
- </execution>
- </executions>
- </plugin>
- <!-- 解决资源文件的编码问题 -->
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-resources-plugin</artifactId>
- <version>2.3</version>
- <configuration>
- <encoding>UTF-8</encoding>
- </configuration>
- </plugin>
执行mvn package以后,会在target下生成可执行jar以及lib,lib里包含依赖的jar.
可执行的jar如果解开看的话会发现里面Main-Class以及Class-Path都已经设置好了,后者指向当前路径的lib路径下