eirslett / frontend-maven-plugin

"Maven-node-grunt-gulp-npm-node-plugin to end all maven-node-grunt-gulp-npm-plugins." A Maven plugin that downloads/installs Node and NPM locally, runs NPM install, Grunt, Gulp and/or Karma.
Apache License 2.0
4.26k stars 870 forks source link

Define node dir on bower install #437

Open erex1 opened 8 years ago

erex1 commented 8 years ago

First of all, great plugin!

Here is my problem:

I have several clients (and more coming) that i want to build with same instance of node and grunt. Therefore i have a created a to module "web-parent" that contains node and grunt. I have placed my two webclients in two submodules. Lets call then "web-client-one" and "web-client-two", these webclients is build by Grunt using the Gruntfile in "web-parent", and they each use their own bower setup.

So my filestructure looks like this:

web-parent/ ├── web-client-one/ │ ├── .bowerrc │ ├── bower.json │ ├── bower │ ├── bower.cmd │ ├── ... │ └── pom.xml ├── web-client-two/ │ ├── .bowerrc │ ├── bower.json │ ├── bower │ ├── bower.cmd │ ├── ... │ └── pom.xml ├── node/ ├── node_modules/ ├── package.json ├── Gruntfile.js ├── ... └── package.json

All this works fine, and I can run "bower install" in both "web-client-one" and "web-client-two" in the cmd thanks to the helper classes bower and bower.cmd that is referencing node and node_modules in the "web-parent".

But this does not work when I try running "bower install" with the plugin. In the pom of "web-client-one" I have this code:

<execution>
    <id>bower install</id>
    <goals>
        <goal>bower</goal>
    </goals>
    <configuration>
        <workingDirectory>.</workingDirectory>
        <installDirectory>../</installDirectory>
        <arguments>install</arguments>
    </configuration>
</execution>

This does not work because "bower install" is trying to fetch node from "web-client-one":

[INFO] Running 'bower install' in c:\test\web-parent\web-client-one
[ERROR] module.js:341
[ERROR]     throw err;
[ERROR]     ^
[ERROR]
[ERROR] Error: Cannot find module ' c:\test\web-parent\web-client-one\node_modules\bower\bin\bower'
[ERROR]     at Function.Module._resolveFilename (module.js:339:15)
[ERROR]     at Function.Module._load (module.js:290:25)
[ERROR]     at Function.Module.runMain (module.js:447:10)
[ERROR]     at startup (node.js:146:18)
[ERROR]     at node.js:404:3

I dont know how the helper classes bower and bower.cmd works, but they doesnt seem to have any effect when running "bower install" with the plugin. Is it shomething I am doing wrong? Or would it be possible to implement the functionality of the helper classes to the plugins configuration? I.e: A value that gives bower install a refrence to the local node and node_modules dir so that it would work the same way as when I am running bower install from cmd in my webclients dir.

eirslett commented 8 years ago

Unfortunately, the plugin isn't written for the use case with multiple modules sharing the same node/npm executables. You could use something like npm with <arguments>run bower</arguments>, and then define a "bower" script in package.json that does what you want.

erex1 commented 8 years ago

Thanks for the fast response! Helped me alot!

Never used npm scripts before, so I didnt think of that at all.

This is what I ended up with:

I placed these package.json files within the client modules:

{
    "private": true,
    "devDependencies": {
    },
    "scripts": {
        "bower": "bower install --config.interactive=false"
    }
}

and changed the bower install part of the clients pom to this:

<execution>
    <id>npm run bower</id>
    <goals>
        <goal>npm</goal>
    </goals>
    <configuration>
        <arguments>run bower</arguments>
        <workingDirectory>.</workingDirectory>
        <installDirectory>../</installDirectory>
    </configuration>
</execution>

Not sure if this is the best way to do this, but it seems to work.

Thanks again!