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.23k stars 869 forks source link

configuration inside execution not working #589

Open arunkjn opened 7 years ago

arunkjn commented 7 years ago

For some weird reason, I am encountering the same problem as mentioned below using 0.0.28, 1.3 and 1.4 versions of this plugin (possibly all versions, not checked)

When I declare execution level configuration it is ignored.

<execution>
    <id>Install node and npm</id>
    <goals>
        <goal>install-node-and-npm</goal>
    </goals>
    <configuration>
        <nodeVersion>v7.4.0</nodeVersion>
        <npmVersion>4.0.5</npmVersion>
        <installDirectory>target</installDirectory>
        <workingDirectory>./</workingDirectory>
    </configuration>
</execution>

The above block will not work.

When I give the configuration in a global configuration block under plugin it works fine.

<plugin>
                    <groupId>com.github.eirslett</groupId>
                    <artifactId>frontend-maven-plugin</artifactId>
                    <!-- Use the latest released version:
                    https://repo1.maven.org/maven2/com/github/eirslett/frontend-maven-plugin/ -->
                    <version>1.4</version>
                    <configuration>
                        <nodeVersion>v7.4.0</nodeVersion>
                        <npmVersion>4.0.5</npmVersion>
                        <installDirectory>target</installDirectory>
                        <workingDirectory>./</workingDirectory>
                        <arguments>-p --config config/webpack.config.prod.js</arguments>
                    </configuration>
                        ...
</plugin>

The above works just fine.

The problem arises when I want to use two npm executions, or an npm execution along with webpack or something else. They do not pick arguments configuration from their own execution block. But work when an arguments config block is given globally. Problem is that all of the goals pick up the last arguments config block from global configuration.

Following is my environment information- Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T22:11:47+05:30) Maven home: /usr/local/Cellar/maven/3.3.9/libexec Java version: 1.8.0_121, vendor: Oracle Corporation Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre Default locale: en_US, platform encoding: UTF-8 OS name: "mac os x", version: "10.12.3", arch: "x86_64", family: "mac"

arunkjn commented 7 years ago

This also seems to be related to issue #294 Please see @demonfiddler 's comments there

arunkjn commented 7 years ago

https://stackoverflow.com/questions/14572632/can-a-maven-plugin-see-the-configuration-tag-from-an-execution-section-autom#

This seems to be related

ghost commented 7 years ago

I'm facing indeed the same issue's it gets complicated when i want to use the arguments option inside configuration.

For example i want to run a certain argument on my gulp build and i want to run an argument on my npm install like so:

<execution>
    <id>npm install</id>
    <goals>
        <goal>npm</goal>
    </goals>
    <configuration>
        <arguments>install --only=production</arguments>
    </configuration>
</execution>
<execution>
    <id>gulp build</id>
    <goals>
        <goal>gulp</goal>
    </goals>
    <configuration>
        <arguments>--brand ${meta.brand}</arguments>
    </configuration>
    <phase>generate-resources</phase>
</execution>

Now if i need to pull out my configuration block onto plugin level these two arguments will interfere with each other.

The argument in gulp build might as well be a specific task that i would like to run instead of the default task, i'm using brand argument there since my gulp task maintains several different sites and based on CI task it will inject the right one to build.

Is there a current work around how i can overcome this?

arunkjn commented 7 years ago

@mickske I ended up using maven-exec-plugin for running gulp/ grunt/ webpack etc tasks. Using this plugin just to install node and npm

ghost commented 7 years ago

wanted to avoid that, i really like this plugin and i didn't want to include a second plugin.

i guess i'll figure something out down the line to see how to overcome this issue.

mriehema commented 7 years ago

As @eirslett already recommended in other issues, I can only support him: Try to run all your tasks via npm scripts. I'm doing the same (for grunt) and it works perfect.

Here is my pom as an example:

            <executions>
<!-- install node and npm -->
              <execution>
                <id>install-node-and-npm</id>
                <goals>
                  <goal>install-node-and-npm</goal>
                </goals>
              </execution>
<!-- npm install -->
              <execution>
                <id>install-node-modules</id>
                <goals>
                  <goal>npm</goal>
                </goals>
                <configuration>
                  <arguments>install --cache-min=Infinity --loglevel=error --no-progress</arguments>
                </configuration>
              </execution>
<!-- npm run production (which calls grunt production) during install phase -->
              <execution>
                <id>build-themes</id>
                <goals>
                  <goal>npm</goal>
                </goals>
                <phase>install</phase>
                <configuration>
                  <arguments>run production</arguments>
                </configuration>
              </execution>
<!-- npm test (which runs mocha and jasmine) during test phase -->
              <execution>
                <id>run-tests</id>
                <goals>
                  <goal>npm</goal>
                </goals>
                <phase>test</phase>
                <configuration>
                  <arguments>test</arguments>
                </configuration>
              </execution>
            </executions>
mriehema commented 7 years ago

duplicates #26.

ghost commented 7 years ago

@mriehema Sorry for the late reply, but your pom doesn't work for me either.

I'm trying to move away from gulp and go the webpack route and do everything through NPM scripts, which is indeed a nicer solution.

If i try to run your exact pom it fails on install-node-and-npm goal already stating this:

Failed to execute goal com.github.eirslett:frontend-maven-plugin:1.4:install-node-and-npm (default-cli) on project globalWeb: The parameters 'nodeVersion' for goal com.github.eirslett:frontend-maven-plugin:1.4:install-node-and-npm are missing or invalid -> [Help 1]

which is due to param nodeVersion missing. Added a configuration block inside the first execution, but it doesn't pick it up either.

if i can't inline any of the configurations block that i won't be able to run npm scripts either since it always uses the and i can't duplicate that since i need several arguments just like your example.

@eirslett You mentioning in the documentation to run most things through NPM scripts as well, could you give me / us a working example of multiple NPM build scripts setup in one pom that works, would be greatly appreciated :)?

Any idea how to make it work, here is my pom:

<plugin>
    <groupId>com.github.eirslett</groupId>
    <artifactId>frontend-maven-plugin</artifactId>
    <version>1.4</version>
    <executions>
        <execution>
            <id>install node and npm</id>
            <goals>
                <goal>install-node-and-npm</goal>
            </goals>
            <configuration>
                <nodeVersion>v7.10.0</nodeVersion>
            </configuration>
        </execution>
        <execution>
            <id>npm install</id>
            <goals>
                <goal>npm</goal>
            </goals>
            <configuration>
                <arguments>install --only=production --projectVersion=${project.version}</arguments>
            </configuration>
        </execution>
        <execution>
            <id>npm build</id>
            <goals>
                <goal>npm</goal>
            </goals>
            <configuration>
                <arguments>run build</arguments>
            </configuration>
        </execution>
    </executions>
</plugin>
ghost commented 7 years ago

After digging a bit deeper and this time actually reading the SO article posted in this issue, turns out if you define the maven phase in the execution blocks the configuration inside the execution block works perfect!!

I think this issue is closeable now, just need to make a note in the docs about this i think!

arunkjn commented 7 years ago

@mickske not sure i get it! Could you please give me an example to elaborate?

ghost commented 7 years ago

Hi @arunkjn

Off course here is my example that seems to be working in jenkins:

<plugin>
    <groupId>com.github.eirslett</groupId>
    <artifactId>frontend-maven-plugin</artifactId>
    <version>1.4</version>
    <configuration>
        <environmentVariables>
            <PROJECT_VERSION>${project.version}</PROJECT_VERSION>
        </environmentVariables>
        <nodeVersion>v7.10.0</nodeVersion>
        <arguments>install --only=production</arguments>
    </configuration>
    <executions>
        <execution>
            <id>install-node-and-npm</id>
            <goals>
                <goal>install-node-and-npm</goal>
            </goals>
            <phase>generate-resources</phase>
        </execution>
        <execution>
            <id>npm-install</id>
            <goals>
                <goal>npm</goal>
            </goals>
            <phase>generate-resources</phase>
        </execution>
        <execution>
            <id>npm-build</id>
            <goals>
                <goal>npm</goal>
            </goals>
            <configuration>
                <arguments>run build</arguments>
            </configuration>
            <phase>generate-resources</phase>
        </execution>
        <execution>
            <id>npm-generate-sass-doc</id>
            <goals>
                <goal>npm</goal>
            </goals>
            <phase>generate-resources</phase>
            <configuration>
                <arguments>run docs:sass</arguments>
            </configuration>
        </execution>
        <execution>
            <id>npm-generate-js-doc</id>
            <goals>
                <goal>npm</goal>
            </goals>
            <phase>generate-resources</phase>
            <configuration>
                <arguments>run docs:js</arguments>
            </configuration>
        </execution>
    </executions>
</plugin>

What did the trick for me was defining the phases in the execution blocks.

Here is a small log from jenkins showing you it kicks off the build process through NPM scripts:

[INFO] --- frontend-maven-plugin:1.4:npm (npm-build) @ web --- [INFO] testFailureIgnore property is ignored in non test phases [INFO] Running 'npm run build' in /app/.jenkins/workspace/TRUNK/web [INFO] [INFO] > Project@1.0.0 build /app/.jenkins/workspace/TRUNK/web [INFO] > NODE_ENV='production' ./node_modules/.bin/webpack --config webpack.config.js -p

arunkjn commented 7 years ago

I have tried including phase information in my execution block before. Didn't work for me!

tonicsoft commented 6 years ago

this seems to be a wider issue with maven, and not related to the front-end-plugin. I have exactly the same problem with a plugin I have written

arunkjn commented 6 years ago

weirdly enough, a coworker setup the exact same config with same maven version in their system and it worked!

tonicsoft commented 6 years ago

A colleague of mine fixed it for me: if you run "mvn plugin-name:goal-name", it doesn't actually run your executions at all, it just runs whatever the default goals are for the plugin. In the example above where the execution is bound to the generate-sources phase, if you run "mvn generate-sources" then the executions should run with their respective configurations. Hope this helps.