有这么一个需求,有一个类有一个main方法,里面有一些业务操作,比如要发送http请求去查询一个数据等等,需要引入第三方依赖包。然后把这个类打成一个jar包,当运行这个jar包后就执行这个类的main方法
项目工程结构
pom.xml
<dependencies> <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.6</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.62</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>11</source> <target>11</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>SambaMount</mainClass> <!-- 替换为你的SambaMount类的全路径 --> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build>
然后执行命令 mvn clean package 打包
将单独的某个java类打成一个jar包(没有依赖,里面有一个main方法)。比如有一个SambaMount.java类
manifest.txt内容有换行符
执行如下命令生成jar文件
javac SambaMount.java
jar cfm SambaMount.jar manifest.txt SambaMount.class