Is it possible to compile grunt project from maven?

后端 未结 9 1285
滥情空心
滥情空心 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:06

    You can use grunt-maven-plugin. It allows you to easily integrate Grunt tasks into Maven build process. No dirty hacks.

    This is what I use in my current project and it works just perfect.

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

    Finally I ended up with this (which is close enough but doesn't solve the problem):

    <plugin>
    <groupId>org.mule.tools.javascript</groupId>
    <artifactId>npm-maven-plugin</artifactId>
    <version>1.0</version>
    <executions>
        <execution>
            <phase>generate-resources</phase>
                <goals>
                    <goal>fetch-modules</goal>
                </goals>
                <configuration>
                    <packages>
                        <package>grunt-cli:0.1.6</package>
                    </packages>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    that installs locally the grunt-cli, but if I don't have installed node.js it's worthless. Although I try to install node.js locally there's the need to have installed python, g++ and make. So I'll go with the KISS solution: install grunt in the build server.

    References:
    https://github.com/mulesoft/npm-maven-plugin
    https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager
    https://github.com/mcheely/requirejs-maven-plugin

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

    You might want to checkout http://jhipster.github.io/ : it's a Yeoman generator, that generates an application which has Maven, Grunt and Bower all working together.

    It's a bit like your third option, but everything is configured for you, which isn't that easy. It's also generating the basic AngularJS and Java REST services for you

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

    This is a full copy/paste solution which work in 2017 using frontend-maven-plugin for front build, and maven-war-plugin to build the war.

    What it does ? install npm, bower grunt,and everything you need, then run npm install, bower install and finally grunt build.

    You can remove/add replace the steps you want, for me it's a full 30 sec install/build library and project.

    <dependencies>
      ...
    </dependencies>
    
    <dependencyManagement>
        <dependencies>
            <!-- https://mvnrepository.com/artifact/com.github.eirslett/frontend-maven-plugin -->
            <dependency>
                <groupId>com.github.eirslett</groupId>
                <artifactId>frontend-maven-plugin</artifactId>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.4</version>
                    <configuration>
                        <warSourceDirectory>src/main/webapp/YourFrontJsFolder/dist</warSourceDirectory>
                        <warName>YouWarName</warName>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
                        <warSourceExcludes>node_modules/**</warSourceExcludes>
                        <includeScope>system</includeScope>
                        <webResources>
                            <resource>
                                <directory>WebContent/WEB-INF</directory>
                                <targetPath>WEB-INF</targetPath>
                                <includes>
                                    <include>**/*.jar</include>
                                    <include>**/*.jsp</include>
                                </includes>
                            </resource>
                        </webResources>
                    </configuration>
                </plugin>
    
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.3</version>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                        <encoding>Cp1252</encoding>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    
        <finalName>YourAppName</finalName>
    </build>
    
    <profiles>
        <profile>
            <id>release</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>com.github.eirslett</groupId>
                        <artifactId>frontend-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <!-- optional: you don't really need execution ids, but it looks 
                                    nice in your build log. -->
                                <id>install node and npm</id>
                                <goals>
                                    <goal>install-node-and-npm</goal>
                                </goals>
                                <!-- optional: default phase is "generate-resources" -->
                                <phase>generate-resources</phase>
    
                                <configuration>
                                    <nodeVersion>v7.6.0</nodeVersion>
                                </configuration>
                            </execution>
    
                            <execution>
                                <id>npm install</id>
                                <goals>
                                    <goal>npm</goal>
                                </goals>
    
                                <!-- optional: default phase is "generate-resources" -->
                                <phase>generate-resources</phase>
    
                                <configuration>
                                    <arguments>install</arguments>
                                </configuration>
                            </execution>
    
                            <execution>
                                <id>bower install</id>
                                <goals>
                                    <goal>bower</goal>
                                </goals>
    
                                <configuration>
                                    <!-- optional: The default argument is actually "install", so unless 
                                        you need to run some other bower command, you can remove this whole <configuration> 
                                        section. -->
                                    <arguments>install</arguments>
                                </configuration>
                            </execution>
    
                            <execution>
                                <id>grunt build</id>
                                <goals>
                                    <goal>grunt</goal>
                                </goals>
    
                                <!-- optional: the default phase is "generate-resources" -->
                                <phase>generate-resources</phase>
    
                                <configuration>
                                    <!-- optional: if not specified, it will run Grunt's default task 
                                        (and you can remove this whole <configuration> section.) -->
                                    <arguments>build</arguments>
                                </configuration>
                            </execution>
                        </executions>
    
                        <configuration>
                            <installDirectory>target</installDirectory>
                            <workingDirectory>src/main/webapp/YourFrontJsFolder</workingDirectory>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>debug</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>IDE</id>
            <activation>
                <property>
                    <name>m2e.version</name>
                </property>
            </activation>
            <build>
                <!-- Put the IDE's build output in a folder other than target, so that 
                    IDE builds don't interact with Maven builds -->
                <directory>target-ide</directory>
            </build>
        </profile>
    </profiles>
    

    Then you can Run as -> Maven build ..., with goal clean install and profile release

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

    If the problem is installing NodeJS on the Jenkins machine then you can use the NodeJS Jenkins plugin.

    https://wiki.jenkins-ci.org/display/JENKINS/NodeJS+Plugin

    We're not using it with Maven (yet) but we've got grunt running.

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

    UPDATE 2014-09-19: This is no longer the most accurate answer - please take a look at some of the other answers below. It was accurate at the time when I answered the question, but there seems to have been a good deal of progress in this area since then.

    I'm afraid you're out of luck. Grunt is built using node and needs to be installed using npm. You might be able to copy an existing installation of Grunt from another machine if you don't want to use npm, but will still use the grunt executable and all of its dependencies on your build server.

    In addition to that, many of the Grunt tasks are implemented as Node.js modules, and you will have to install them as well. Again, you might be able to copy them from another server, where you've done the Node.js/Grunt installation, but at one point, you have to do it.

    For running Grunt from Maven, your best bet is to use the Maven exec plugin and then execute the grunt executable from there.

    As an alternative, there are several Maven plugins that allow you to do things similar to Grunt in a Java-based fashion. They require additional configuration not compatible with Grunt, so YMMV. One that I've used in the past is http://code.google.com/p/wro4j/, which comes with a Maven plugin as well: http://code.google.com/p/wro4j/wiki/MavenPlugin

    Any particular reason why you can't install Node.js on your build server?

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