[Java] 利用Eclipse將 gradle project 打包做war檔

預設下Gradle 會將project 以jar 的形式打包, 然而, 若要打包的是web application的話, jar 並不能於Tomcat 或者JBoss 中發佈, 故war 檔格式雖然舊但仍有其價值. 所以須要透過plugin 去進行. (所以個人討厭寫Java 其中一個原因就是太多相類似的plugin)

  1. 加入library 及dependency.
    因為spring-boot-starter-web 本身已經有tomcat container, 所以可以將之排除.
    在build.gradle 中, 加入以下粗字表示的句子:

    buildscript {
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.3.RELEASE")
        }
    }
    
    apply plugin: 'java'
    apply plugin: 'eclipse'
    apply plugin: 'idea'
    apply plugin: 'org.springframework.boot'
    apply plugin: 'io.spring.dependency-management'
    apply plugin: 'war'
    
    jar {
        baseName = 'gs-handling-form-submission'
        version =  '0.1.0'
    }
    
    repositories {
        mavenCentral()
    }
    
    sourceCompatibility = 1.8
    targetCompatibility = 1.8
    
    dependencies {
        compile("org.springframework.boot:spring-boot-starter-web")
        providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
        compile("org.springframework.boot:spring-boot-starter-thymeleaf")
        testCompile("junit:junit")
    }令
  2. 在application.java 中, 加入以下method:
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    	      return application.sources(Application.class);
    	}
  3. 更新Gradle project 設定, 以獲得library 及Gradle command.
    於project 中按右鍵 > Gradle Refresh Gradle Project.
  4. 建立war 檔.
    於Gradle Task 視窗內, 右鍵選取build > Run Gradle Task.
  5. 檢查結果.  
    執行成功的話, war 檔會放在%ProjectDir%\build\libs\ 內.
About C.H. Ling 262 Articles
a .net / Java developer from Hong Kong and currently located in United Kingdom. Thanks for Google because it solve many technical problems so I build this blog as return. Besides coding and trying advance technology, hiking and traveling is other favorite to me, so I will write down something what I see and what I feel during it. Happy reading!!!

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.