springboot项目的打包部署(gralde和maven)
来源:互联网 发布:程序员表情包 编辑:程序博客网 时间:2024/06/05 03:33
前言
不管是maven 构建的项目,还是gralde构建的项目,springboot目录结构都是一致的。公司很多同事用的是maven,但是由于gralde配置上比较简洁,而且性能比maven要快,而且又可以连接maven资源库,因此我更倾向于gralde工具。在项目开发中我都会提供pom.xml和build.gralde两种配置文件,供其他同事选择使用。下面介绍如何使用gralde 和maven对项目进行打包部署。
项目结构如下图所示,创建了一个支持maven 和gradle的项目,现在需要将项目打包成jar包部署到linux服务器上。需要注意的是,由于构建工具默认打包方式会把依赖的jar包和配置文件也会打包进去,这样对于系统升级维护,和配置文件的更改,都非常不利。 因此部署到测试或生产环境,一定要将class文件和三方jar分开。
运行jar需要的配置文件
jar包 的运行需要MANIFEST.MF文件,该文件包含一些启动所需的信息 :
Manifest-Version 用来定义manifest文件的版本
Implementation-Title 定义了扩展实现的标题
Created-By 文件生成者
Class-Path 应用程序或者类装载器使用该值来构建内部的类搜索路径
Main-Class jar启动类
Implementation-Version 定义扩展实现的版本
Implementation-Vendor 定义扩展实现的组织
Implementation-Vendor-Id 定义扩展实现的组织的标识
Implementation-URL : 定义该扩展包的下载地址(URL)
Specification-Title 定义扩展规范的标题
Specification-Version 定义扩展规范的版本
Specification-Vendor 声明了维护该规范的组织
Sealed 定义jar文件是否封存,值可以是true或者false (这点我还不是很理解)
创建打包任务配置
下面提供了两种打包方式的文件配置,同时提供了完整的配置文件,大家在使用构建工具创建项目的时候可以参考。打包的配置脚本使用(//------------------创建打包任务------- ) 进行了标注。本文所述项目都采用前后端分离方式,没有使用jsp和freemaker前端模板引擎。如果项目使用了这些资源文件也不需要打包到jar里,部署时候放在jar包同级目录下,同时在配置文件中指定jsp或ftl文件的加载路径即可。
gralde配置
buildscript {ext {springBootVersion = '1.5.4.RELEASE'}repositories {maven{url 'http://192.168.0.210:8081/nexus/content/repositories/thirdparty/'}maven{url 'http://192.168.0.210:8081/nexus/content/repositories/central/'}}dependencies {classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")}}apply plugin: 'java'apply plugin: 'idea'apply plugin: 'org.springframework.boot'//版本version = '1.0.0'//java代码的版本兼容性sourceCompatibility = 1.8//编译编码tasks.withType(JavaCompile){ options.encoding="utf-8"}//maven资源库地址repositories {maven{url 'http://192.168.0.210:8081/nexus/content/repositories/thirdparty/'}maven{url 'http://192.168.0.210:8081/nexus/content/repositories/central/'}}//扩展字段ext{dubboVersion="2.5.4"commonLangVersion="3.2"nettyVersion="3.2.5.Final"ehcacheVersion="2.10.4"}//依赖dependencies {compile('org.springframework.boot:spring-boot-starter-web')compile('org.springframework.boot:spring-boot-starter-data-jpa')compile('org.springframework.boot:spring-boot-starter-tomcat')compile("com.alibaba:dubbo:${dubboVersion}")compile('mysql:mysql-connector-java:5.1.27')compile ("org.apache.commons:commons-lang3:${commonLangVersion}")compile "net.sf.ehcache:ehcache:${ehcacheVersion}"compile 'org.hibernate:hibernate-ehcache:'compile 'com.github.sgroschupf:zkclient:0.1'compile 'log4j:log4j:1.2.16'compile "org.jboss.netty:netty:${nettyVersion}"testCompile('org.springframework.boot:spring-boot-starter-test') //引用WEB-INF下jar包 // compile fileTree(dir:'src/main/webapp/WEB-INF/lib',include:['*.jar'])}//生成doc文档task generateJavadoc(type: Javadoc) {source = sourceSets.main.allJava//指定编码,否则会报错options {encoding "utf-8"charSet 'utf-8'author trueversion true}}//设置工程源sourceSets {main {resources {srcDir 'src/main'} //资源文件输入目录(classCompile也会使用此目录)output.resourcesDir = "build/classes/main"}}//-----------------------创建打包任务--------------------------------------------//sync 继承 Copy ,将三方jar复制到该目录下 task copyProjectJar(type: Sync){from configurations.runtime,configurations.compileinto 'build/libs/lib'}//生成jartask generateJar(type: Jar, dependsOn: [copyProjectJar, jar]) {manifest {attributes ('Implementation-Title': 'Gradle','Implementation-Version': version,'Created-By': 'chenyao','Main-Class': 'com.test.TcsApplication','Class-Path': configurations.compile.collect {'lib/' + it.getName()}.join(' '))}into('') {from "$buildDir/classes/main"include('com/')}into('') {from "$projectDir/src/main/resources"include ('dubbo.xml','*.txt')}}//-----------------------创建打包任务--------------------------------------------//发布到maven远程仓库apply plugin: 'maven'//apply plugin: 'war'uploadArchives {repositories.mavenDeployer {repository(url: "http://localhost:8081/repository/project/") {authentication(userName: "admin", password: "admin123")}pom.groupId = "com.tcs"pom.version = "1.0-SNAPSHOT"pom.artifactId = "tcs"}}//单元测试task myTest(type: Test) {exclude()filter {//测试指定包includeTestsMatching "com.test.service.*"//指定测试类includeTestsMatching "*ServiceTest"}}
maven 配置
<?xml version="1.0" encoding="UTF-8"?><project xmlns="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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.test</groupId> <artifactId>tcs</artifactId> <version>2.2.0</version> <packaging>jar</packaging> <name>tcs</name> <description>tcs for spring boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.6</java.version> <ehcache.version>2.10.4</ehcache.version> <project.package.directory>target</project.package.directory> </properties> <repositories> <repository> <id>Central</id> <url>http://192.168.0.210:8081/nexus/content/repositories/central/</url> </repository> <repository> <id>3rd party</id> <url>http://192.168.0.210:8081/nexus/content/repositories/thirdparty/</url> </repository> </repositories> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>1.5.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>1.5.4.RELEASE</version> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.27</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> <version>1.5.4.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.4</version> </dependency> <dependency> <groupId>org.jboss.netty</groupId> <artifactId>netty</artifactId> <version>3.2.5.Final</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.7.3</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.39</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <version>1.5.4.RELEASE</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <!--跳过测试--> <skipTests>true</skipTests> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin>//-----------------------创建打包任务-------------------------------------------- <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.6</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <!--指定启动类--> <mainClass>com.test.TcsApplication</mainClass> </manifest> </archive> <!--指定classes目录--> <!--<classesDirectory>src/main/java</classesDirectory>--> <outputDirectory>${project.package.directory}/${project.artifactId}</outputDirectory> <!--排除classes目录下不需要打包的文件--> <excludes> <exclude>META-INF/**</exclude> <exclude>application.properties</exclude> <exclude>logback-spring.xml</exclude> </excludes> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.6</version> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <includeScope>compile</includeScope> <!--复制依赖jar到指令目录--> <outputDirectory>${project.package.directory}/${project.artifactId}/lib</outputDirectory> </configuration> </execution> </executions> </plugin> <!--复制资源文件到指定目录--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.6</version> <executions> <execution> <id>copy-resources</id> <phase>package</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${project.package.directory}/${project.artifactId}/config/</outputDirectory> <resources> <resource> <!--资源文件的目录--> <directory>src/main/resources/</directory> </resource> </resources> </configuration> </execution> </executions> </plugin> </plugins> <!--指定资源文件打包到jar中--> <!--<resources>--> <!--<resource>--> <!--<directory>src/main/resources</directory>--> <!--<includes>--> <!--<include>dubbo.xml</include>--> <!--</includes>--> <!--</resource>--> <!--</resources>-->//-----------------------创建打包任务-------------------------------------------- </build></project>
- springboot项目的打包部署(gralde和maven)
- indea创建部署gralde项目
- maven web项目打包部署
- SpringBoot项目打包部署外部JavaEE容器
- SpringBoot打包jar、war部署项目
- maven打包springboot项目跳过test文件夹
- 使用tomcatPlugin配合JRebel解决maven项目依赖war的打包和热部署的问题
- .Net 中实现项目的安装和部署(打包)
- Maven入门(二)项目的打包和运行
- Spring项目打包部署( Maven Assembly插件)
- Springboot中如何引入本地jar包,并通过maven把项目成功打包成jar包部署
- maven的配置和项目部署(1)
- Idea-Maven项目的创建和部署
- 利用gralde和Andriod Studio进行多渠道打包
- maven项目自动打包部署到tomcat
- Java Maven项目打包部署流程
- Jenkins配置-打包部署maven项目
- Maven 项目自动化打包部署(Jboss & Tomcat)
- PyCharm安装第三方模块Request、BeautifulSoup
- node-red学习第三篇-firstflow
- 【css布局】display、position、float、overflow
- 分布式
- Linux 命令行
- springboot项目的打包部署(gralde和maven)
- Could not create the view: An unexpected exception was thrown.问题解决
- vue使用resource传参数
- 题目100-1的个数
- spring-bean命名方式-以及实例化--配置文件整合
- loadrunner录制脚本常见问题
- Java使用RSA加密解密签名及校验
- centos 下github的详细教程,创建,提交等
- HTML下js使用方式