天天看點

搭建非maven spring boot項目 并且idea進行打包

1.所需jar包

搭建非maven spring boot項目 并且idea進行打包
搭建非maven spring boot項目 并且idea進行打包

2.搭建web項目

搭建非maven spring boot項目 并且idea進行打包

3

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class SpringBootTemplateApplications {

    public static void main(String[] args) {

    //SpringBoot程式啟動入口

        SpringApplication.run(SpringBootTemplateApplications.class, args);

    }

}

4

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RestController;

@RestController

public class SampleController {

    //處理請求位址映射的注解

    @RequestMapping(value="/hello",method=RequestMethod.GET)

    public String hello() {

    //相應的内容

        return "hello World";

    }

}

5 啟動

搭建非maven spring boot項目 并且idea進行打包

通路

搭建非maven spring boot項目 并且idea進行打包

IDEA 打包項目

一、檢查項目是否包含META-INF檔案夾。若包含META-INF檔案夾,将其删除。等待重新生成。

搭建非maven spring boot項目 并且idea進行打包

二、建構Artifacts

2.1 選擇菜單中的 File->Project Structure 。 在彈出的Project Structure中選擇 Artifacts-> + -> JAR ->From modules with dependencies

搭建非maven spring boot項目 并且idea進行打包

2.2 在彈出的視窗中選擇要打包的子產品和主函數,然後選擇要打成散包還是一個整體。

搭建非maven spring boot項目 并且idea進行打包

Module: 子產品,選擇需要打包的子產品。如果程式沒有分子產品,那麼隻有一個可以選擇的。

MainClass:選擇程式的入口類。

extract to the target JAR:抽取到目标JAR。選擇該項則會将所依賴的jar包全都打到一個jar檔案中。

copy to the output directory and link via manifest:将依賴的jar複制到輸出目錄并且使用manifest連結它們。

Direct for META-INF/MANIFEST.MF: 如果上面選擇了 "copy to ... "這一項,這裡需要選擇生成的manifest檔案在哪個目錄下。

Include tests: 是否包含tests。 一般這裡不選即可。

選擇OK,會在剛才選擇的檔案夾下面生成一個META-INF檔案夾,下面有一個MANIFEST.MF檔案

搭建非maven spring boot項目 并且idea進行打包

MANIFEST.MF

主要一下幾個:

Manifest-Version: Manifest檔案的版本,這個不用管。

Class-Path: 描述lib包相對生成的jar的路徑。

Main-Class: 程式的入口類

三、将生成的MANIFEST.MF增添到JAR包

搭建非maven spring boot項目 并且idea進行打包

在這些jar包中,黃色的是依賴的jar包,藍色的是我們生成的要運作的jar包。在藍色的jar包上右鍵,選擇 “Create Directory”(建立檔案夾),名稱叫做META-INF。

搭建非maven spring boot項目 并且idea進行打包

在建立的“META-INF”檔案夾上右鍵,選擇Add Copy of 下的 File,選擇剛剛生成的MANIFEST.MF檔案。如下圖 點選确定

搭建非maven spring boot項目 并且idea進行打包

點選确定

搭建非maven spring boot項目 并且idea進行打包

四、生成Jar包。

配置完上述後。選擇菜單中的 build -> build artifacts...

搭建非maven spring boot項目 并且idea進行打包
搭建非maven spring boot項目 并且idea進行打包

此時頁面中間會彈出要生成的jar包,選擇剛剛建構的Artifacts,選擇build或者rebuild

Build:隻将主Jar包重新生成,不重新生成所依賴的Jar包。

Rebuild: 将所有jar包重新生成。

此時生成jar包已經完成

搭建非maven spring boot項目 并且idea進行打包
搭建非maven spring boot項目 并且idea進行打包

Jar包啟動

cmd 進入到檔案夾下 java -jar xxx.jar

搭建非maven spring boot項目 并且idea進行打包

繼續閱讀