Spring-boot生成可执行jar包
2016-06-21 11:58
591 查看
弄一个可执行文件耗费了一上午的时间,确实挺郁闷的。把调试过程中遇到的一些问题记录一下pom.xml配置
<?xmlversion="1.0"encoding="UTF-8"?>
<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 'target='_blank'>http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>dbtest</groupId><artifactId>dbtest</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>数据库测试</name><description>Spring-boot数据库测试程序</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.3.0.M1</version><relativePath/><!--lookupparentfromrepository--></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.4</version></dependency><dependency><groupId>com.oracle</groupId><artifactId>ojdbc14</artifactId><version>10.2.0.4.0</version><scope>runtime</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><mainClass>main.Application</mainClass></configuration><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.7</source><target>1.7</target><showWarnings>true</showWarnings></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId></plugin></plugins></build><repositories><repository><id>spring-snapshots</id><name>SpringSnapshots</name><url>https://repo.spring.io/snapshot</url><snapshots><enabled>true</enabled></snapshots></repository><repository><id>spring-milestones</id><name>SpringMilestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories><pluginRepositories><pluginRepository><id>spring-snapshots</id><name>SpringSnapshots</name><url>https://repo.spring.io/snapshot</url><snapshots><enabled>true</enabled></snapshots></pluginRepository><pluginRepository><id>spring-milestones</id><name>SpringMilestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></pluginRepository></pluginRepositories></project>[/code]代码编写如下:用于测试数据库连接是否正常packagemain;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.SQLException;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;importorg.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;importorg.springframework.boot.context.web.SpringBootServletInitializer;importorg.springframework.util.NumberUtils;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;@SpringBootApplication@RestControllerpublicclassApplicationextendsSpringBootServletInitializerimplementsEmbeddedServletContainerCustomizer{privatestaticPropertiesLoaderloader=newPropertiesLoader("test.properties");@RequestMapping("/")publicStringgreeting(){StringBuffermessage=newStringBuffer();Stringtype=loader.getProperty("jdbc.type");Stringdriver=loader.getProperty("jdbc.driver");Stringurl=loader.getProperty("jdbc.url");Stringusername=loader.getProperty("jdbc.username");Stringpassword=loader.getProperty("jdbc.password");if(type.equals("oracle")){try{message.append("正在加载驱动:"+type+"\n");Class.forName(driver);message.append("驱动加载成功!\n");}catch(ClassNotFoundExceptionex){return"驱动加载错误!"+ex.getMessage();}Connectionconn=null;try{message.append("用户名:"+username+"\n");message.append("密码:"+password+"\n");conn=DriverManager.getConnection(url,username,password);System.out.println(conn);message.append("连接登录成功:"+conn+"\n");}catch(SQLExceptione){e.printStackTrace();return"连接异常"+e.getMessage();}finally{try{conn.close();}catch(SQLExceptione){e.printStackTrace();return"关闭异常"+e.getMessage();}}}message.append("测试成功!!!\n");returnmessage.toString();}publicstaticvoidmain(String[]args){SpringApplication.run(Application.class,args);}@Overridepublicvoidcustomize(ConfigurableEmbeddedServletContainercontainer){//通过这个参数配置,可以更改端口号//NumberUtils.parseNumber(loader.getProperty("host.post"),Integer.class)System.out.println(loader.getProperty("host.post"));container.setPort(8081);}}[/code]这里需要注意:一定要有一个包名,如果没有包名也会报错。用到的工具类packagemain;importjava.io.IOException;importjava.io.InputStream;importjava.util.NoSuchElementException;importjava.util.Properties;importorg.apache.commons.io.IOUtils;importorg.springframework.core.io.DefaultResourceLoader;importorg.springframework.core.io.Resource;importorg.springframework.core.io.ResourceLoader;publicclassPropertiesLoader{privatestaticResourceLoaderresourceLoader=newDefaultResourceLoader();privatefinalPropertiesproperties;publicPropertiesLoader(String...resourcesPaths){properties=loadProperties(resourcesPaths);}publicPropertiesgetProperties(){returnproperties;}/***取出Property,但以System的Property优先,取不到返回空字符串.*/privateStringgetValue(Stringkey){StringsystemProperty=System.getProperty(key);if(systemProperty!=null){returnsystemProperty;}if(properties.containsKey(key)){returnproperties.getProperty(key);}return"";}/***取出String类型的Property,但以System的Property优先,如果都为Null则抛出异常.*/publicStringgetProperty(Stringkey){Stringvalue=getValue(key);if(value==null){thrownewNoSuchElementException();}returnvalue;}/***取出String类型的Property,但以System的Property优先.如果都为Null则返回Default值.*/publicStringgetProperty(Stringkey,StringdefaultValue){Stringvalue=getValue(key);returnvalue!=null?value:defaultValue;}/***取出Integer类型的Property,但以System的Property优先.如果都为Null或内容错误则抛出异常.*/publicIntegergetInteger(Stringkey){Stringvalue=getValue(key);if(value==null){thrownewNoSuchElementException();}returnInteger.valueOf(value);}/***取出Integer类型的Property,但以System的Property优先.如果都为Null则返回Default值,如果内容错误则抛出异常*/publicIntegergetInteger(Stringkey,IntegerdefaultValue){Stringvalue=getValue(key);returnvalue!=null?Integer.valueOf(value):defaultValue;}/***取出Double类型的Property,但以System的Property优先.如果都为Null或内容错误则抛出异常.*/publicDoublegetDouble(Stringkey){Stringvalue=getValue(key);if(value==null){thrownewNoSuchElementException();}returnDouble.valueOf(value);}/***取出Double类型的Property,但以System的Property优先.如果都为Null则返回Default值,如果内容错误则抛出异常*/publicDoublegetDouble(Stringkey,IntegerdefaultValue){Stringvalue=getValue(key);returnvalue!=null?Double.valueOf(value):defaultValue;}/***取出Boolean类型的Property,但以System的Property优先.如果都为Null抛出异常,如果内容不是true/false则返回false.*/publicBooleangetBoolean(Stringkey){Stringvalue=getValue(key);if(value==null){thrownewNoSuchElementException();}returnBoolean.valueOf(value);}/***取出Boolean类型的Property,但以System的Property优先.如果都为Null则返回Default值,如果内容不为true/false则返回false.*/publicBooleangetBoolean(Stringkey,booleandefaultValue){Stringvalue=getValue(key);returnvalue!=null?Boolean.valueOf(value):defaultValue;}/***载入多个文件,文件路径使用SpringResource格式.*/privatePropertiesloadProperties(String...resourcesPaths){Propertiesprops=newProperties();for(Stringlocation:resourcesPaths){//logger.debug("Loadingpropertiesfilefrom:"+location);InputStreamis=null;try{Resourceresource=resourceLoader.getResource(location);is=resource.getInputStream();props.load(is);}catch(IOExceptionex){}finally{IOUtils.closeQuietly(is);}}returnprops;}}[/code]配置文件:test.properties#oracledatabasesettingsjdbc.type=oraclejdbc.driver=oracle.jdbc.driver.OracleDriverjdbc.url=jdbc:oracle:thin:@1.0.4.103:1521:orcljdbc.username=jeesitejdbc.password=jeesite#mysqldatabasesetting#jdbc.type=mysql#jdbc.driver=com.mysql.jdbc.Driver#jdbc.url=jdbc:mysql://localhost:3306/qrcode?useUnicode=true&characterEncoding=utf-8#jdbc.username=root#jdbc.password=toorhost.post=8081[/code]1、项目用到了什么spring-boot的版本,就用spring-boot的工具进行打包。这点很重要呢例如v<version>1.3.0.M1</version>机器里面装了很多版本,一定要找对才行。 [插入]配置MavenBuild-RunConfigurations如下图所示 其中Goals:位置为org.apache.maven.plugins:maven-jar-plugin:2.5:jarorg.springframework.boot:spring-boot-maven-plugin:1.3.0.M1:repackage必须选择先maven后spring-boot-maven这个是有先后顺序的呢。不这样搞不行。 成功的话会生成如下图所示: 这两个缺一不可部。源代码:
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- Oracle数据库学习总结
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- Linux 自检和 SystemTap
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序