簡體   English   中英

angular2彈簧靴項目結構

[英]angular2 spring-boot project structure

我有一個新的angular2項目,該項目使用快速入門中描述的標准文件結構構建。 我正在嘗試構建一個API網關,並讓我的應用程序通過spring-boot托管,但是我無法將boot配置為在創建生成源的項目中使用/ dist目錄。 項目結構如下:

project  
|--dist  
|--node_modules
|--src  
|  |--app  
|  |--assets
|  |--main  
|  |  |--java  
|  |  |--resources
|  |  |  |--config  
|  |  |--webapp
|  |  |  |--WEB-INF  

我想使用默認的/ dist目錄,以便仍可以使用npm / webpack在UI上進行連續開發。

我嘗試像這樣配置靜態資源目錄:

 spring.resources.staticLocations: /dist

但這似乎不起作用。

我創建了一個資源處理程序以直接指向dist目錄:

@Configuration
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {

  @Override
  public void addResourceHandlers(final ResourceHandlerRegistry registry) {

    String currentPath = new File("./").getAbsolutePath();
    currentPath = "file:///" + currentPath;

   registry.addResourceHandler("/**").addResourceLocations(currentPath + "/dist/");
  }
}  

這解決了部分問題,但是現在我的根URL('/')不再映射到index.html。

有沒有更簡單/更好的方法來配置spring-boot來查找/ dist項目目錄? 我的項目結構應該改變嗎? 我真的很想讓所有這些部分整齊地工作。

Spring Boot將自動添加位於以下任何目錄中的靜態Web資源:

/META-INF/resources/

/resources/

/static/

/public/

文件夾相對於src / main / resources

如果按如下所示放置index.html,則spring無需任何配置即可提供文件。

src/main/resources/META-INF/resources/index.html

src/main/resources/resources/index.html

src/main/resources/static/index.html

src/main/resources/public/index.html

我接受了@chrylis的建議,並指出了要構建/分發(使用Gradle)的webpack構建的目標目錄。

我避免將生成的源放在/resources/**因為最終它將被內置到.war中並部署到企業應用服務器中-我並不想在我的構建中包含很多包含/排除邏輯來支持這一點。 另外,對於生成的.js源代碼,build目錄似乎更合乎邏輯。

我的項目結構現在看起來像這樣:

project  
|--build
|  |--dist
|  |--...  
|--node_modules
|--src  
|  |--app  
|  |--assets
|  |--main  
|  |  |--java  
|  |  |--resources
|  |  |  |--config  
|  |  |--webapp
|  |  |  |--WEB-INF  

我取消了WebMvcConfigurerAdapter並將靜態資源配置為指向構建中的dist目錄:

spring.resources.static-locations: "file:./build/dist/"  

現在,我可以運行Webpack,Spring-Boot或將其部署到我的應用程序服務器中,並且使用相同的源代碼,並且配置開銷很小。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM