基于很多人不知道怎样打包成jar直接运行
今晚来总结一下:
采用assembly 目录如下:
---bin {start.sh;stop.sh}
---config
---webapp
---lib
assembly:
<assembly> <id>${project.version}</id> <formats> <format>tar.gz</format> </formats> <baseDirectory>${project.artifactId}-${project.version}</baseDirectory> <includeBaseDirectory>false</includeBaseDirectory> <fileSets> <fileSet> <directory>target/classes</directory> <outputDirectory>/config</outputDirectory> <includes> <include>*.properties</include> <include>*.yml</include> <include>*.xml</include> <include>*.png</include> <!-- <include>log4j.xml</include> --> </includes> <fileMode>0644</fileMode> </fileSet> <fileSet> <directory>src/main/assembly/bin</directory> <outputDirectory>bin</outputDirectory> <fileMode>0755</fileMode> </fileSet> <fileSet> <directory>src/main/assembly/config</directory> <outputDirectory>config</outputDirectory> <fileMode>0644</fileMode> </fileSet> <fileSet> <directory>src/main/webapp</directory> <outputDirectory>webapp</outputDirectory> <fileMode>0644</fileMode> </fileSet> <fileSet> <directory>target</directory> <outputDirectory>lib</outputDirectory> <includes> <include>*.jar</include> </includes> </fileSet> <fileSet> <directory>${project.build.directory}</directory> <outputDirectory>/</outputDirectory> <includes> <include>*.jar</include> </includes> </fileSet> </fileSets> <dependencySets> <dependencySet> <outputDirectory>lib</outputDirectory> </dependencySet> </dependencySets> </assembly>
记得把war 改为jar
<plugin> <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-jar-plugin --> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.1.0</version> <configuration> <classesDirectory>target/classes/</classesDirectory> <archive> <addMavenDescriptor>false</addMavenDescriptor> <manifest> <addClasspath>true</addClasspath> <mainClass>com.jfinal.club.StartService</mainClass> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> </manifest> <manifestEntries> <Class-Path>./</Class-Path> </manifestEntries> </archive> <excludes> <exclude>**/*.properties</exclude> <exclude>**/*.yml</exclude> <exclude>**/*.xml</exclude> </excludes> </configuration> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>3.0.0</version> <configuration> <appendAssemblyId>false</appendAssemblyId> <archive> <manifest> <addClasspath>true</addClasspath> <mainClass>com.jfinal.club.StartService</mainClass> </manifest> </archive> <descriptors> <descriptor>src/main/assembly/assembly.xml</descriptor> </descriptors> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
程序入口:
public static void main(String[] args) { /** * 特别注意:IDEA 之下建议的启动方式,仅比 eclipse 之下少了最后一个参数 */ String baseBath = String.valueOf(StartService.class.getProtectionDomain().getCodeSource().getLocation()); String classPath, webRootPath, jarPath; if (StrKit.notBlank(baseBath) && baseBath.contains("file:/")) { // 获取运行操作系统的运行方式 window和linux的细微区别 boolean windows = System.getProperties().getProperty("os.name").contains("Windows"); System.out.println(System.getProperties().getProperty("os.name")); jarPath = (windows ? "" : "/") + baseBath.substring("file:/".length()); classPath = (windows ? "" : "/") + jarPath.substring(0, jarPath.lastIndexOf("/")) + "/webapp"; System.out.println("jarPath:" + jarPath); System.out.println("classPath:" + classPath); webRootPath = classPath; // ZipUtil.unzip(jarPath, classPath); // 这两步是核心指定 webapp目录和classpath目录 PathKit.setWebRootPath(webRootPath); PathKit.setRootClassPath(classPath); // eclipse 启动是4个参数 JFinal.start(webRootPath, 8009, "/"); } else { throw new RuntimeException("扑街路径不对!"); } }
这里感谢:http://www.jfinal.com/share/422
mac 跑是没有问题,没有试过win 上跑,希望能帮到其他人。