最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Springboot項(xiàng)目與vue項(xiàng)目整合打包的實(shí)現(xiàn)方式

 更新時(shí)間:2019年07月03日 08:54:49   作者:Andy·Hu  
這篇文章主要介紹了Springboot項(xiàng)目與vue項(xiàng)目整合打包的實(shí)現(xiàn)方式,本文通過(guò)兩種方式給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

我的環(huán)境

* JDK 1.8
 * maven 3.6.0
 * node環(huán)境

1.為什么需要前后端項(xiàng)目開(kāi)發(fā)時(shí)分離,部署時(shí)合并?

在一些公司,部署實(shí)施人員的技術(shù)無(wú)法和互聯(lián)網(wǎng)公司的運(yùn)維團(tuán)隊(duì)相比,由于各種不定的環(huán)境也無(wú)法做到自動(dòng)構(gòu)建,容器化部署等。因此在這種情況下盡量減少部署時(shí)的服務(wù)軟件需求,打出的包數(shù)量也盡量少。針對(duì)這種情況這里采用的在開(kāi)發(fā)中做到前后端獨(dú)立開(kāi)發(fā),打包時(shí)在后端springboot打包發(fā)布時(shí)將前端的構(gòu)建輸出一起打入,最后只需部署springboot的項(xiàng)目即可,無(wú)需再安裝nginx服務(wù)器

在這里我有兩種方式,一種是簡(jiǎn)單的,一種是復(fù)雜的,下邊先來(lái)看一個(gè)簡(jiǎn)單的例子:

1)前端開(kāi)發(fā)好后將build構(gòu)建好的dist下的文件拷貝到springboot的resources的static下(boot項(xiàng)目默認(rèn)沒(méi)有static,需要自己新建)

操作步驟:前端vue項(xiàng)目使用命令 npm run build 或者 cnpm run build 打包生成dist文件,在springboot項(xiàng)目中resources下建立static文件夾,將dist文件中的文件復(fù)制到static中,然后去application中跑起來(lái)boot項(xiàng)目,這樣直接訪問(wèn)index.html就可以訪問(wèn)到頁(yè)面(api接口請(qǐng)求地址自己根據(jù)情況打包時(shí)配置或者在生成的dist文件中config文件夾中的index.js中配置)

項(xiàng)目結(jié)構(gòu)如圖:

鼠標(biāo)選中的這幾個(gè)就是從dist文件中復(fù)制過(guò)來(lái)的前端的包,然后我們直接啟動(dòng)boot項(xiàng)目就可以通過(guò)index.html訪問(wèn)了(ps:上面這是最簡(jiǎn)單的合并方式,但是如果作為工程級(jí)的項(xiàng)目開(kāi)發(fā),并不推薦使用手工合并,也不推薦將前端代碼構(gòu)建后提交到springboot的resouce下,好的方式應(yīng)該是保持前后端完全獨(dú)立開(kāi)發(fā)代碼,項(xiàng)目代碼互不影響,借助jenkins這樣的構(gòu)建工具在構(gòu)建springboot時(shí)觸發(fā)前端構(gòu)建并編寫自動(dòng)化腳本將前端webpack構(gòu)建好的資源拷貝到springboot下再進(jìn)行jar的打包,最后就得到了一個(gè)完全包含前后端的springboot項(xiàng)目了。不過(guò)上述方法操作簡(jiǎn)單,便于使用,如果想一次性打包完成的話,就看第二種)

2:第二種方法是在src/main下建立一個(gè)webapp文件夾,然后將前端項(xiàng)目的源文件復(fù)制到該文件夾下,具體結(jié)構(gòu)如圖:

然后使用maven命令執(zhí)行本地node打包命令,這樣就可以 在執(zhí)行mvn clean package命令時(shí),利用maven插件執(zhí)行cnpm run build命令(我是使用的淘寶的鏡像cnpm,國(guó)外的npm命令會(huì)相對(duì)慢一些,大家根據(jù)自己的條件選擇,具體命令請(qǐng)看項(xiàng)目中前端vue文件的README.md),一次性完成整個(gè)過(guò)程

實(shí)現(xiàn)方法是這樣的,我們要引入org.codehaus.mojo插件來(lái)進(jìn)行maven調(diào)用node命令,pom.xml中為:

<plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <executions>
          <execution>
            <id>exec-cnpm-install</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>exec</goal>
            </goals>
            <configuration>
              <executable>${cnpm}</executable>
              <arguments>
                <argument>install</argument>
              </arguments>
              <workingDirectory>${basedir}/src/main/webapp</workingDirectory>
            </configuration>
          </execution>
          <execution>
            <id>exec-cnpm-run-build</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>exec</goal>
            </goals>
            <configuration>
              <executable>${cnpm}</executable>
              <arguments>
                <argument>run</argument>
                <argument>build</argument>
              </arguments>
              <workingDirectory>${basedir}/src/main/webapp</workingDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>

然后maven-resources-plugin插件將項(xiàng)目的前端文件打包到boot項(xiàng)目的classes中,具體的請(qǐng)參考pom.xml中的,

 將webapp/dist文件夾中的文件復(fù)制到src/main/resources/static中,并打包到classes

<!--copy文件到指定目錄下 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <configuration>
          <encoding>${project.build.sourceEncoding}</encoding>
        </configuration>
        <executions>
          <execution>
            <id>copy-spring-boot-webapp</id>
            <!-- here the phase you need -->
            <phase>validate</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
              <encoding>utf-8</encoding>
              <outputDirectory>${basedir}/src/main/resources/static</outputDirectory>
              <resources>
                <resource>
                  <directory>${basedir}/src/main/webapp/dist</directory>
                </resource>
              </resources>
            </configuration>
          </execution>
        </executions>
      </plugin>

然后通過(guò)maven命令:

mvn clean package -P window 

打包成功后我們的前端項(xiàng)目就整個(gè)的打包到了boot項(xiàng)目的jar包中,然后啟動(dòng)項(xiàng)目,訪問(wèn)index.html頁(yè)面就看到了項(xiàng)目啟動(dòng)成功。

打出來(lái)的jar包中如果static說(shuō)明打包由于種種原因失敗了,我就遇到過(guò)幾次,這時(shí)候需要再來(lái)一次 mvn clean package -P window

ps:下面看下SprintBoot 整合vue實(shí)現(xiàn)上傳下載

這里記錄一下在Springboot中實(shí)現(xiàn)文件上傳下載的核心代碼

package com.file.demo.springbootfile;
import com.file.util.ResultUtil;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.apache.tomcat.util.http.fileupload.util.Streams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
/*
* springboot整合vue,文件上傳下載
* */
//上傳不要用@Controller,用@RestController
@RestController
@RequestMapping("/file")
public class FileController {
  private static final Logger logger = LoggerFactory.getLogger(FileController.class);
  //在文件中,不用/或者\(yùn)最好,推薦使用File.separator
  private final static String fileDir="files";
  private final static String rootPath = System.getProperty("user.home")+ File.separator+fileDir+File.separator;
  /*
  * 文件上傳
  * */
  @RequestMapping("/upload")
  public Object uploadFile(@RequestParam("file")MultipartFile[] multipartFiles, final HttpServletResponse response, final HttpServletRequest request){
    File fileDir = new File(rootPath);
    /*
    * exists():測(cè)試此抽象路徑名表示的文件是否存在
    * isDirectory():檢查一個(gè)對(duì)象是否是文件夾
    * isFile():判斷是否為文件,也可能是文件目錄
    * */
    if(!fileDir.exists() && !fileDir.isDirectory()){
      //檢查到文件不存在則創(chuàng)建
      fileDir.mkdir();//創(chuàng)建文件,一級(jí)
      fileDir.mkdirs();//創(chuàng)建多級(jí)
    }
    try{
      if(multipartFiles != null && multipartFiles.length > 0){
        for ( int i = 0;i<multipartFiles.length;i++){
          try{
            String storagePath = rootPath+multipartFiles[i].getOriginalFilename();
            logger.info("上傳的文件:" + multipartFiles[i].getName()+","+multipartFiles[i].getContentType()+","
                +multipartFiles[i].getOriginalFilename() + ",保持的路徑為:" + storagePath);
            Streams.copy(multipartFiles[i].getInputStream(), new FileOutputStream(storagePath), true);
          }catch (IOException e){
            logger.info(ExceptionUtils.getFullStackTrace(e));
          }
        }
      }
    }catch (Exception e){
      return ResultUtil.error(e.getMessage());
    }
    return ResultUtil.success("上傳成功!");
  }
  /*
  * 文件下載
  * */
  @RequestMapping("/download")
  public Object downkiadFile(@RequestParam String fileName,final HttpServletResponse response,final HttpServletRequest request){
    OutputStream os = null;
    InputStream is = null;
    try{
      //獲取輸出流rootPath
      os = response.getOutputStream();
      //重置輸出流
      response.reset();
      response.setContentType("application/x-download;charset=GBK");
      response.setHeader("Content-Disposition", "attachment;filename="+ new String(fileName.getBytes("utf-8"), "iso-8859-1"));
      //讀取流
      File f= new File(rootPath+fileName);
      is = new FileInputStream(f);
      if(is == null){
        logger.error("下載文件失敗,請(qǐng)檢查文件“"+ fileName +"”是否存在");
        return ResultUtil.error("下載附件失敗,請(qǐng)檢查文件“" + fileName + "”是否存在");
      }
      //復(fù)制文件
      IOUtils.copy(is,response.getOutputStream());
      //刷新緩沖
      response.getOutputStream().flush();
    }catch (IOException e){
      return ResultUtil.error("下載文件失敗,error:" + e.getMessage());
    }
    //文件的關(guān)閉在finally中執(zhí)行
    finally {
      {
        try {
          if(is != null){
            is.close();
          }
        }catch (IOException e){
          logger.error(ExceptionUtils.getFullStackTrace(e));
        }
        try{
          if(os != null){
            os.close();
          }
        }catch (IOException e){
          logger.info(ExceptionUtils.getFullStackTrace(e));
        }
      }
    }
    return null;
  }
}

源碼下載地址: https://github.com/struggle0903/SpringBootfiledemo.git

總結(jié)

以上所述是小編給大家介紹的Springboot項(xiàng)目與vue項(xiàng)目整合打包的實(shí)現(xiàn)方式,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

相關(guān)文章

最新評(píng)論

正蓝旗| 威远县| 井冈山市| 民乐县| 丰县| 大埔县| 兴海县| 龙门县| 泸定县| 都江堰市| 山东| 农安县| 阿拉善左旗| 万荣县| 罗城| 洞头县| 高邮市| 辛集市| 西贡区| 个旧市| 商南县| 施秉县| 社旗县| 建昌县| 靖边县| 隆安县| 南宫市| 犍为县| 晋中市| 岢岚县| 夏邑县| 和田市| 岱山县| 平塘县| 城市| 西充县| 台湾省| 吉林省| 长沙县| 苍梧县| 苗栗县|