Maven JAR插件跳过generated-resources文件夹

时间:2017-11-09 05:31:55

标签: angularjs spring maven spring-boot

在尝试运行一个简单的AngularJS Spring Boot项目时(在解决this thread中提到的问题之后),我遇到了一个奇怪的问题。以下是我的Maven POM。我面临的问题是,虽然Wro4j插件能够在 generated-resources 下生成JS / CSS文件,尽管在 build / resources 下我已经提供了要打包的路径, generated-resources 下的文件没有打包到“ static ”文件夹下的JAR中(只显示我的自定义JS文件和index.html文件)那里)。怎么能纠正这个?

    <modelVersion>4.0.0</modelVersion>
    <artifactId>spring-angularjs</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>  

    <parent>
        <groupId>org.test</groupId>
        <artifactId>my-microservice-parent</artifactId>
        <version>0.1.0-SNAPSHOT</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <wro4j.version>1.8.0</wro4j.version>
        <java.version>1.8</java.version>
        <bootstrap.version>3.3.7-1</bootstrap.version>
        <angularjs.version>1.5.9</angularjs.version>
        <jquery.version>3.1.1-1</jquery.version>
    </properties>

    <dependencies>  
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>angularjs</artifactId>
            <version>${angularjs.version}</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jasmine</artifactId>
            <version>2.0.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
    </dependencies> 

    <build>
        <resources>         
            <resource>
                <directory>${project.build.directory}/generated-resources</directory>
            </resource>
            <resource>
                <directory>${project.basedir}/src/main/resources</directory>
            </resource>
        </resources>
        <plugins>           
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <!-- Serves *only* to filter the wro.xml so it can get an absolute 
                            path for the project -->
                        <id>copy-resources</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}/target/wro</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>src/main/wro</directory>
                                    <filtering>true</filtering>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>ro.isdc.wro4j</groupId>
                <artifactId>wro4j-maven-plugin</artifactId>
                <version>${wro4j.version}</version> 
                <executions>
                  <execution>
                    <phase>compile</phase>
                    <goals>
                      <goal>run</goal>
                    </goals>
                  </execution>
                </executions>
                <configuration>
                    <wroManagerFactory>ro.isdc.wro.maven.plugin.manager.factory.ConfigurableWroManagerFactory</wroManagerFactory>
                    <cssDestinationFolder>${project.build.directory}/generated-resources/static/css</cssDestinationFolder>
                    <jsDestinationFolder>${project.build.directory}/generated-resources/static/js</jsDestinationFolder>
                    <wroFile>${project.build.directory}/wro/wro.xml</wroFile>
                    <!--
                    <extraConfigFile>${basedir}/src/main/wro/wro.properties</extraConfigFile>
                    <contextFolder>${basedir}/src/main/wro</contextFolder>
                    -->
                    <extraConfigFile>${project.build.directory}/wro/wro.properties</extraConfigFile>
                    <contextFolder>${project.build.directory}/wro</contextFolder>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.webjars</groupId>
                        <artifactId>jquery</artifactId>
                        <version>${jquery.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.webjars</groupId>
                        <artifactId>angularjs</artifactId>
                        <version>${angularjs.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.webjars</groupId>
                        <artifactId>bootstrap</artifactId>
                        <version>${bootstrap.version}</version>
                    </dependency>
                </dependencies>
            </plugin>

            <plugin>
                <groupId>com.github.searls</groupId>
                <artifactId>jasmine-maven-plugin</artifactId>
                <version>2.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>test</goal>
                        </goals>
                    </execution>
                </executions>       
                <dependencies>
                    <dependency>
                        <groupId>org.webjars</groupId>
                        <artifactId>angularjs</artifactId>
                        <version>${angularjs.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

</project>

以下是Maven构建日志(对于 mvn clean package ):

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building spring-angularjs 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ spring-angularjs ---
[INFO] Deleting C:\projects\spring-angularjs\target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:copy-resources (copy-resources) @ spring-angularjs ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 3 resources
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ spring-angularjs ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\projects\spring-angularjs\target\generated-resources
[INFO] Copying 3 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ spring-angularjs ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to C:\projects\spring-angularjs\target\classes
[INFO] 
[INFO] --- wro4j-maven-plugin:1.8.0:run (default) @ spring-angularjs ---
[INFO] C:\projects\spring-angularjs\target/wro
[INFO] Executing the mojo: 
[INFO] Wro4j Model path: C:\projects\spring-angularjs\target\wro\wro.xml
[INFO] targetGroups: null
[INFO] minimize: true
[INFO] ignoreMissingResources: null
[INFO] parallelProcessing: false
[INFO] buildDirectory: C:\projects\spring-angularjs\target
[INFO] destinationFolder: C:\projects\spring-angularjs\target
[INFO] jsDestinationFolder: C:\projects\spring-angularjs\target\generated-resources\static\js
[INFO] cssDestinationFolder: C:\projects\spring-angularjs\target\generated-resources\static\css
[INFO] The following groups will be processed: [angular-bootstrap]
[INFO] folder: C:\projects\spring-angularjs\target\generated-resources\static\css
[INFO] processing group: angular-bootstrap.css
[WARNING] Less warnings are:
[WARNING] 10:1 Cannot link source map. Css result location is not know and could not be deduced from input less source..
[INFO] file size: angular-bootstrap.css -> 145074 bytes
[INFO] C:\projects\spring-angularjs\target\generated-resources\static\css\angular-bootstrap.css (145074 bytes)
[INFO] folder: C:\projects\spring-angularjs\target\generated-resources\static\js
[INFO] processing group: angular-bootstrap.js
[INFO] file size: angular-bootstrap.js -> 254638 bytes
[INFO] C:\projects\spring-angularjs\target\generated-resources\static\js\angular-bootstrap.js (254638 bytes)
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ spring-angularjs ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\projects\spring-angularjs\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ spring-angularjs ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ spring-angularjs ---
[INFO] No tests to run.
[INFO] 
[INFO] --- jasmine-maven-plugin:2.0:test (default) @ spring-angularjs ---
[INFO] jetty-8.1.14.v20131031
[INFO] Started SelectChannelConnector@0.0.0.0:60402
[INFO] Executing Jasmine Specs
[INFO] Resolved artifact C:\Users\XXXXX\.m2\repository\com\github\klieber\phantomjs\2.0.0\phantomjs-2.0.0-windows.zip from remote-repos-vr (https://vrartifactory.eficode.com/remote-repos-vr, default, releases+snapshots)
[INFO] Extracting C:\Users\XXXXX\.m2\repository\com\github\klieber\phantomjs\2.0.0\phantomjs-2.0.0-windows.zip\phantomjs-2.0.0-windows\bin\phantomjs.exe to C:\projects\spring-angularjs\target\phantomjs\phantomjs-2.0.0-windows\bin\phantomjs.exe
[INFO] 
-------------------------------------------------------
 J A S M I N E   S P E C S
-------------------------------------------------------
[INFO] 

Results: 0 specs, 0 failures, 0 pending

[INFO] stopped o.e.j.s.h.ContextHandler{/webjars,file:/C:/projects/spring-angularjs/}
[INFO] stopped o.e.j.s.h.ContextHandler{/classpath,file:/C:/projects/spring-angularjs/}
[INFO] stopped o.e.j.s.h.ContextHandler{/,file:/C:/projects/spring-angularjs/}
[INFO] stopped o.e.j.s.h.ContextHandler{/spec,file:/C:/projects/spring-angularjs/}
[INFO] stopped o.e.j.s.h.ContextHandler{/src,file:/C:/projects/spring-angularjs/}
[INFO] 
[INFO] --- maven-jar-plugin:2.5:jar (default-jar) @ spring-angularjs ---
[INFO] Building jar: C:\projects\spring-angularjs\target\spring-angularjs-0.0.1-SNAPSHOT.jar
[INFO] 
[INFO] --- spring-boot-maven-plugin:1.3.8.RELEASE:repackage (default) @ spring-angularjs ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 32.848 s
[INFO] Finished at: 2017-11-09T16:04:39+05:30
[INFO] Final Memory: 98M/503M
[INFO] ------------------------------------------------------------------------

1 个答案:

答案 0 :(得分:1)

wro4j-maven-plugin:run阶段之后,

compile正在process-resources阶段运行,其中所有资源都被复制到${project.build.outputDirectory}(默认情况下为target/classes)。只有来自此文件夹的文件才会包含在您的JAR中,但您的CSS / JS文件是在 Maven尝试将它们复制到outputDirectory后生成的,因此永远不会被添加。

您只需将Wro4j插件配置为在generate-resources阶段运行:

       <plugin>
            <groupId>ro.isdc.wro4j</groupId>
            <artifactId>wro4j-maven-plugin</artifactId>
            <version>${wro4j.version}</version> 
            <executions>
              <execution>
                <phase>generate-resources</phase>
                <goals>
                  <goal>run</goal>
                </goals>
              </execution>
            </executions>
       ...
       </plugin>

如果由于某些原因导致您的文件无法在compile之前生成(可能需要编译代码),您可以使用Maven Resources插件手动将资源添加到项目构建输出目录中之后生成,就像您在评论中一样生成 - 但我建议您在prepare-package阶段使用${project.build.outputDirectory}执行此操作,例如:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.0.2</version>
    <execution>
        <id>copy-resources-post-compile</id>
        <phase>prepare-package</phase>
        <goals>
            <goal>copy-resources</goal>
        </goals>
        <configuration>
            <outputDirectory>${project.build.outputDirectory}</outputDir‌​ector‌​y>
            <resources>
                <resource>
                    <directory>${project.build.directory}/generated-res‌​ources</directory>
                </resource>
            </resources>
        </configuration>
    </execution>
</plugin>

编辑:要了解有关Maven生命周期,阶段和目标的更多信息,请参阅Intro to Build Lifecyclethis tutorial。另请查看default bindings以查看默认情况下运行的插件,具体取决于您的打包。