Is it possible to compile grunt project from maven?

后端 未结 9 1286
滥情空心
滥情空心 2021-01-30 16:55

I\'m trying to execute grunt tasks from within maven without needing to install Node.js or anything. This is because I wan\'t my artifact to be packaged by Jenkins and I can\'t

相关标签:
9条回答
  • 2021-01-30 17:28

    Can be done with exec-maven-plugin.

    Define a script and dependency to grunt-cli in your package.json:

    ...
      "scripts": {
        "build": "./node_modules/.bin/grunt install"
      },
      "devDependencies": {
      "grunt-cli": "^1.2.0",
    ...
    

    In your pom, add the commands to run:

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>X.Y.Z</version>
                <executions>
                    <execution>
                        <id>exec-npm-install</id>
                        <phase>generate-sources</phase>
                        <configuration>
                            <workingDirectory>${project.basedir}</workingDirectory>
                            <executable>npm</executable>
                            <arguments>
                                <argument>install</argument>
                            </arguments>
                        </configuration>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>exec-grunt-install</id>
                        <phase>generate-sources</phase>
                        <configuration>
                            <workingDirectory>${project.basedir}</workingDirectory>
                            <executable>npm</executable>
                            <arguments>
                                <argument>run</argument>
                                <argument>build</argument>
                            </arguments>
                        </configuration>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
    

    It will now run on mvn package

    0 讨论(0)
  • 2021-01-30 17:29

    The first problem is that Maven is Java, but Grunt.js runs on the Node.js runtime. The easiest integration I ever achieved between the two involved the maven-exec-plugin. The maven-exec-plugin is capable of executing .sh/.bat/.cmd scripts, whichever are native to the OS you are using. So during a Maven build I would have the maven-exec-plugin execute a script named optimize-js.sh, for example, which would simply do something like “grunt release –force”, or whatever. The scripts can be made to do whatever. The important thing is to configure the maven-exec-plugin to execute them in the correct working directory. Of course, “grunt” and “node” need to be executable from the command-line.

    0 讨论(0)
  • 2021-01-30 17:31

    Yes, using the frontend-maven-plugin, you can compile Grunt projects via Maven (found via the NodeJS mailing list).

    As the documentation points out, the plugin has the following features:

    • Let you keep your frontend and backend builds as separate as possible, by reducing the amount of interaction between them to the bare minimum; using only 1 plugin.
    • Let you use Node.js and its libraries in your build process without installing Node/NPM globally for your build system
    • Let you ensure that the version of Node and NPM being run is the same in every build environment

    I've walked through the code and it's fairly simple. Thank goodness someone finally put this together; it's an elegant solution. The repository includes an example that uses a regular Gruntfile.js to invoke jshint analysis.

    0 讨论(0)
提交回复
热议问题