2016-11-30 81 views
3

在使用Angular 2 fronend部署基於Java的應用程序時,我遇到了一個問題。在Heroku上部署Java應用程序和Angular2 fronend

我的應用程序需要安裝NodeJs,它是「npm install」以從package.json中獲取所有依賴關係。 而只有與maven。 Angular2應用程序位於java webapp文件夾中。 我看到buildpack安裝它。但他們不工作。例如 這個

https://github.com/Vincit/heroku-buildpack-java-nodejs

它搜索* .ts文件的根,而不是在「Web應用程序」 java文件夾。 有沒有一些智能buildpacks這樣的任務,或其他簡單的方法來做到這一點?

預先感謝您!

回答

0

你可以用Heroku對multiple buildpacks on an app的支持來做到這一點。總之,運行:

$ heroku buildpacks:clear 
$ heroku buildpacks:add heroku/nodejs 
$ heroku buildpacks:add heroku/java 

在你的情況,這種情況可能是非常相似的this JHipster example,它使用的Node.js和角度。有幾件事情需要考慮:

  • Heroku Node.js buildpack will not install devDependencies by default。您需要將它們移動到dependencies或設置配置變量NPM_CONFIG_PRODUCTION=false
  • 如果您在運行時不需要您的node_modules目錄或Node.js運行時,它將使您的應用程序變得巨大。你可以使用Maven刪除它們。

下面是一個例子:

<plugin> 
    <artifactId>maven-clean-plugin</artifactId> 
    <version>2.5</version> 
    <executions> 
    <execution> 
     <id>clean-build-artifacts</id> 
     <phase>install</phase> 
     <goals><goal>clean</goal></goals> 
     <configuration> 
     <excludeDefaultDirectories>true</excludeDefaultDirectories> 
     <filesets> 
      <fileset> 
      <directory>node_modules</directory> 
      </fileset> 
      <fileset> 
      <directory>.heroku/node</directory> 
      </fileset> 
     </filesets> 
     </configuration> 
    </execution> 
    </executions> 
</plugin> 

這在更詳細地描述JHipster post

0

我使用這個插件:

https://github.com/eirslett/frontend-maven-plugin

<plugin> 
    <groupId>com.github.eirslett</groupId> 
    <artifactId>frontend-maven-plugin</artifactId> 
    <version>1.0</version> 
    <executions> 
     <execution> 
     <id>install node and npm</id> 
     <goals> 
     <goal>install-node-and-npm</goal> 
     </goals> 
     <configuration> 
     <nodeVersion>v4.4.3</nodeVersion> 
     <npmVersion>3.8.3</npmVersion>          
     </configuration> 
    </execution> 
    <execution> 
     <id>npm install</id> 
     <goals> 
     <goal>npm</goal> 
     </goals> 
     <configuration> 
     <arguments>--strict-ssl=false install</arguments> 
     </configuration> 
     </execution>        
     <execution> 
     <id>npm build prod</id> 
     <goals> 
     <goal>npm</goal> 
     </goals> 
    <configuration> 
    <arguments>run build.prod</arguments> 
    </configuration> 
    </execution> 
    </executions> 
    </plugin> 

NPM build.prod是我一飲而盡任務,建立督促部署和被指定爲我的package.json(腳本我使用角2號種子):https://github.com/mgechev/angular-seed

我不得不創建一個任務複製到一個地方,我的Java應用程序使用靜態文件:

import * as gulp from 'gulp'; 
let gnf = require('gulp-npm-files'); 
let resourceDest = 'src/main/resources/public'; 

export =() => { 

     gulp.src('**', {cwd:'./dist/prod'}).pipe(gulp.dest(resourceDest)); 
     gulp.src(gnf(), {base:'./'}).pipe(gulp.dest(resourceDest)); 


}; 

這複製我編譯的角2 JavaScript到src/main/resources/public