简体   繁体   English

使用Angular2 fronend在Heroku Java应用程序上进行部署

[英]Deploying on Heroku Java app with Angular2 fronend

i was faced with a problem, during deploying Java based app with Angular 2 fronend. 在使用Angular 2 fronend部署基于Java的应用程序时,我遇到了一个问题。

My app needs to be installed with NodeJs and it's "npm install" to fetch all it's dependencies from package.json. 我的应用程序需要安装NodeJs并且它是“npm install”以从package.json获取它的所有依赖项。 And only than with maven. 而且只有maven。 Angular2 app is located in java webapp folder. Angular2 app位于java webapp文件夹中。 I saw buildpacks to install it. 我看到buildpacks来安装它。 But they dont work. 但他们不工作。 For example this one 例如这一个

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

It searches for *.ts files in the root, but not in the "webapp" java folder. 它在根目录中搜索* .ts文件,但不在“webapp”java文件夹中搜索。 Is there some smart buildpacks for task like that, or others convinient ways to do it simply? 有没有像这样的任务的智能构建包,或其他方便的方式来做到这一点?

Thank you in advance! 先感谢您!

You can do this with Heroku's support for multiple buildpacks on an app . 您可以使用Heroku 对应用程序上多个buildpack的支持来实现此目的。 In short, run: 简而言之,运行:

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

In your case, the situation is probably very similar to this JHipster example , which uses Node.js and angular. 在您的情况下,情况可能与此JHipster示例非常相似, 该示例使用Node.js和angular。 There are a few things to consider: 有几点需要考虑:

  • The Heroku Node.js buildpack will not install devDependencies by default . 默认情况下 ,Heroku Node.js buildpack 不会安装devDependencies You either need to move them to dependencies or set the config var NPM_CONFIG_PRODUCTION=false . 您需要将它们移动到dependencies或设置config var NPM_CONFIG_PRODUCTION=false
  • If you don't need your node_modules directory or Node.js runtime at runtime, it will make your app huge. 如果您在运行时不需要node_modules目录或Node.js运行时,它将使您的应用程序变得庞大。 You can use Maven to remove them. 您可以使用Maven删除它们。

Here's an example: 这是一个例子:

<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>

This is described in more detail in the JHipster post . 这在JHipster帖子中有更详细的描述。

I use this plugin: 我用这个插件:

https://github.com/eirslett/frontend-maven-plugin 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 is my gulp task to build for prod deployment and is specified as a script in my package.json (I'm using the angular 2 seed): https://github.com/mgechev/angular-seed npm build.prod是我为prod部署构建的gulp任务,在我的package.json中指定为脚本(我使用的是角度2种子): https//github.com/mgechev/angular-seed

I had to create a task to copy to a place for my java app to use the files statically: 我必须创建一个任务来复制到我的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));


};

This copies my compiled angular 2 javascript into src/main/resources/public 这将我编译的angular 2 javascript复制到src / main / resources / public中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM