一、编写Main方法 html
import java.util.Date; /** * 描述 : * * @Author : zhanghao * @Time : 2019/10/28 14:08 */ public class ***MainTest { public static void main(String[] args) { ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-config.xml"); ***API ***ServiceAPI= (***ServiceAPI) applicationContext.getBean("****Impl"); System.out.println("定时任务执行结果_"+ DateUtils.format(new Date())+":"+***ServiceAPI.***Process()); } }
具体项目结构以下:java
二、经过idea能够快速进行Main方法的jar包封装,具体步骤能够参考spring
https://blog.csdn.net/yuang12345/article/details/90293013apache
http://www.javashuo.com/article/p-qvmbcsof-no.htmlapi
http://www.javashuo.com/article/p-zaptcznp-cq.htmlapp
三、编译完成后 执行 java -jar ****MainTest 便可运行相关方法maven
估计大几率,会执行报错(偷笑)ide
错误一:不识别java命令工具
检查是否正确配置了java的环境变量url
错误二:***Test.jar中没有主清单属性
使用解压工具打开jar包,找到META-INF-->MANIFEST.MF->使用记事本打开,在第二行添加
Main-Class: main方法的路径
保存后从新执行命令便可
错误三:执行过程当中报 Invalid signature file digest for Manifest main attributes
将打好包的jar文件中的 META-INF/*.RSA META-INF/*.DSA META-INF/*.SF 文件删掉
参考连接:http://www.javashuo.com/article/p-drlpeiio-dg.html
https://blog.csdn.net/dai451954706/article/details/50086295
错误四:执行过程当中报 org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/context]
或者 其它的schema错误
因为没有配置META-INF/spring.handlers和META-INF/spring.schemas,因此若是工程中依赖了Spring的多个依赖,在打包时后面的会把前面的覆盖,使得这两个文件中永远只保存最后一个spring依赖的schema和handler。
方法一:使用maven的打包插件
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version> 1.7.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.handlers</resource> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.schemas</resource> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.chenzhou.test.Main</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin>
方法二:
第二种方法其实很简单,找到这三个文件,用我提供的这三个完整的文件覆盖便可。
一般,这三个文件都在jar包下面的META-INF路径下
参考连接:https://www.iteye.com/blog/chenzhou123520-1971322
https://www.iteye.com/blog/chenzhou123520-1706242
http://www.javashuo.com/article/p-notqopys-dy.html
错误五:找不到 ***.propertie或者spring-config.xml
maven打包插件配置错误,resources下面的配置没有打进来