如何仅将maven执行定位到build文件夹?

时间:2015-01-15 12:22:31

标签: node.js maven gruntjs war bower

我有一个使用Maven和frontend-maven-plugin(com.github.eirslett)的项目。

当我运行mvn install插件中的所有执行时,他们会在node_modules根目录中创建bower_componentsnodesrc/main/webapp个文件夹,实际的前端代码是。

问题是,我希望mvn install只执行warbuild目录中生成的包,而不是在版本化的应用程序代码中创建,就像它一样Java库。

有没有办法实现这个目标?

这是我pom.xml的相关部分:

<build>
    <directory>build</directory>
    <outputDirectory>build/classes</outputDirectory>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
                <encoding>UTF-8</encoding>
                <webResources>
                    <resource>
                        <filtering>true</filtering>
                        <directory>src/main/webapp</directory>
                        <includes>
                            <include>WEB-INF/weblogic.xml</include>
                        </includes>
                    </resource>
                </webResources>
            </configuration>
        </plugin>
        ...
        <plugin>
            <groupId>com.github.eirslett</groupId>
            <artifactId>frontend-maven-plugin</artifactId>
            <version>0.0.20</version>

            <configuration>
                <workingDirectory>src/main/webapp</workingDirectory>
            </configuration>

            <executions>
                <execution>
                    <id>install node and npm</id>
                    <goals>
                        <goal>install-node-and-npm</goal>
                    </goals>
                    <configuration>
                        <nodeVersion>v0.10.34</nodeVersion>
                        <npmVersion>2.1.11</npmVersion>
                    </configuration>
                </execution>

                <execution>
                    <id>npm install</id>
                    <goals>
                        <goal>npm</goal>
                    </goals>
                    <configuration>
                        <arguments>install</arguments>
                    </configuration>
                </execution>

                <execution>
                    <id>bower install</id>
                    <goals>
                        <goal>bower</goal>
                    </goals>
                    <configuration>
                        <arguments>install</arguments>
                    </configuration>
                </execution>

                <execution>
                    <id>grunt build</id>
                    <goals>
                        <goal>grunt</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        ...
    </plugins>
</build>

2 个答案:

答案 0 :(得分:1)

插件(frontend-maven-plugin)主要支持此功能。 &#34; workingDirectory&#34;参数告诉插件在哪里做工作(即npm安装)。这需要构建文件(即package.json,gruntFile.js)在该workingDirectory中。为了适应这种情况,我添加了一个执行执行,以便在构建的其余部分之前复制这些文件(使用过滤)。我的grunt文件然后在适当的时候从源引用文件,任何输出都在我的target-grunt文件夹中。

这是我的配置:

        <plugin>
            <groupId>com.github.eirslett</groupId>
            <artifactId>frontend-maven-plugin</artifactId>
            <version>0.0.20</version>
            <configuration>
                <workingDirectory>target-grunt</workingDirectory>
            </configuration>
            <executions>
               ...
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <id>prepare-grunt</id>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <target>
                            <!-- copy, filter, rename -->
                            <filter token="moduleName" value="${moduleName}" />
                            <filter token="project.version" value="${project.version}" />
                            <filter token="project.artifactId" value="${project.artifactId}" />
                            <copy file="${basedir}/target-grunt/imported-js/main/js/com/verisk/underwriting/config/grunt/npm-package-module/0.0.1/npm-package-module-0.0.1.js" tofile="${basedir}/target-grunt/package.json" filtering="true" failonerror="true" verbose="true" />
                            <copy file="${basedir}/Gruntfile.js" tofile="${basedir}/target-grunt/Gruntfile.js" failonerror="true" verbose="true" />
                        </target>
                    </configuration>
                </execution>
            </executions>
        </plugin>

答案 1 :(得分:0)

一种不弄脏前端资源目录的可能方法,例如来自src/main/frontendResources的GIT(或其他源回购),其中包含生成的node_modules,bower_components,...,目录和文件在npm期间,凉亭和咕unt声执行是:

  1. 使用maven-resources-plugin复制源目录src/main/frontendResources/target/webappStagingDir子文件夹中 初始化阶段
  2. 设置workingDirectoryinstallDirectory中的frontend-maven-plugin/target/webappStagingDir
  3. 将Bower输出目录设置为 /target/webappStagingDir的子文件夹。
    例如放置

    {“目录”:     “ bower_components”}

    在.bowerrc文件中

  4. 建立凉亭后,使用 maven-resources-plugin,复制包含在其中的输出内置源 在/target/${artifactId}-${version}期间将bower_components文件夹 准备打包阶段(在战争之前)

这是pom切片:

...
<plugin>
    <groupId>com.github.eirslett</groupId>
    <artifactId>frontend-maven-plugin</artifactId>
    <version>1.6</version>

    <configuration>
        <workingDirectory>${basedir}/target/frontendResourcesStagingDir</workingDirectory>
        <installDirectory>${basedir}/target/frontendResourcesStagingDir</installDirectory>
    </configuration>

    <executions>
        <execution>
            <id>install node and npm</id>
            <phase>generate-resources</phase>
            <goals>
                <goal>install-node-and-npm</goal>
            </goals>
            <configuration>
                <nodeVersion>v8.12.0</nodeVersion>
                <npmVersion>6.4.1</npmVersion>
            </configuration>
        </execution>        
        <execution>
            <id>npm install</id>
            <phase>generate-resources</phase>
            <goals>
                <goal>npm</goal>
            </goals>
            <configuration>
                <arguments>install</arguments>
            </configuration>
        </execution>
        <execution>
            <id>bower install</id>
            <phase>generate-resources</phase>
            <goals>
                <goal>bower</goal>
            </goals>
            <configuration>
                <arguments>install</arguments>
            </configuration>
        </execution>
        <execution>
            <id>grunt build</id>
            <phase>generate-resources</phase>
            <goals>
                <goal>grunt</goal>
            </goals>
            <configuration>
                <arguments>build</arguments>
            </configuration>
        </execution>
    </executions>
</plugin>

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <!-- 
            Copy of the /frontendResources directory, coming from GIT, in /target directory
            executed before the compiling and build of frontend resources
            That dir will be the working directory of npm, bower, grunt
            in order to avoid the creation of the direcotries node, node_modules, bower_components, ...
            (they may be committed in repository)
        -->
        <execution>
            <id>copy-frontendResources-toStagingDir-beforeBuild</id>
            <phase>initialize</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${basedir}/target/frontendResourcesStagingDir</outputDirectory>
                <resources>          
                    <resource>
                        <directory>${basedir}/src/main/frontendResources</directory>
                        <filtering>true</filtering>
                    </resource>
                </resources>              
            </configuration>            
        </execution>
        <!-- 
            Copy of the /frontendResourcesStagingDir/grunt_output directory in /target/ directory
            executed after build of frontend resources and before the war pachage creation
            it contains the output of grunt install execution
        -->
        <execution>
            <id>copy-frontendResources-afterBuild</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${basedir}/target/${artifactId}-${version}</outputDirectory>
                <resources>          
                    <resource>
                        <directory>${basedir}/target/frontendResourcesStagingDir/grunt_output</directory>
                        <filtering>true</filtering>
                    </resource>
                </resources>              
            </configuration>            
        </execution>
    </executions>
</plugin>
...

对于在Node.js中具有前端并且在Java中具有仅一个.war文件作为输出可交付项的后端的混合项目,此解决方案也很有用

相关问题