通过m2e自动更新生成的css文件

时间:2014-05-07 15:01:43

标签: eclipse maven less maven-plugin m2eclipse

我正在使用lesscss-maven-plugin为目标目录(target\generated-sources)生成不同的css文件,然后使用maven-war-plugin将此目录添加为webResouce。这些文件将完全正常。

但m2e-plugin(版本1.0.0)在已更改时,不会复制相应网络资源文件夹(m2e-wtp\web-resources)中的这些文件。只有当我运行eclipse“maven / update project”时,才会更新更改。但我希望更改在文件更改时自动生效。这是我的pom配置:

<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
    <lifecycleMappingMetadata>
        <pluginExecutions><pluginExecution>
                <pluginExecutionFilter>
                    <groupId>org.lesscss</groupId>
                    <artifactId>lesscss-maven-plugin</artifactId>
                    <versionRange>[1.3.3]</versionRange>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </pluginExecutionFilter>
                <action>
                    <execute>
                        <runOnIncremental>true</runOnIncremental>
                        <runOnConfiguration>true</runOnConfiguration>
                    </execute>
                </action>
            </pluginExecution>  
        </pluginExecutions>
    </lifecycleMappingMetadata>
</configuration>

...

<plugin>
    <groupId>org.lesscss</groupId>
    <artifactId>lesscss-maven-plugin</artifactId>
    <version>1.3.3</version>
    <configuration>
        <sourceDirectory>${project.basedir}/src/main/less</sourceDirectory>
        <outputDirectory>${project.build.directory}/generated-sources/styles</outputDirectory>
        <lessJs>${project.basedir}/tools/less/less-1.7.0.min.js</lessJs>
        <includes>
            <include>backend/backend-main.less</include>
            <include>frontend/frontend-main.less</include>
        </includes>
    </configuration>
    <executions>
        <execution>
            <phase>process-resources</phase>
            <goals>
                <goal>compile</goal>
            </goals>
        </execution>
    </executions>
</plugin>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.4</version>
    <configuration>              
        <webResources>
            <resource>
                <directory>${project.build.directory}/generated-sources/styles</directory>
                <targetPath>styles</targetPath>
            </resource>
        </webResources>
    </configuration>
</plugin>

2 个答案:

答案 0 :(得分:4)

有两种选择:

使用m2e特定配置文件

这与您找到的解决方法类似,但更简洁,因为它仅在m2e上激活配置文件,并且您使用属性为不使用m2e打包时设置替代值。

您创建一个m2e特定的配置文件,激活后,会将文件直接放在m2e-wtp/web-resources/styles目录中

<properties>      
    <lesscss.outputDirectory>${project.build.directory}/${project.build.finalName}/styles</lesscss.outputDirectory>
</properties>

<profiles>
    <profile>
        <id>m2e</id>
            <activation>
                <property>
                    <name>m2e.version</name>
                </property>
            </activation>
            <properties>
                <lesscss.outputDirectory>${project.build.directory}/m2e-wtp/web-resources/styles</lesscss.outputDirectory>
            </properties>
    </profile>
</profiles>

<plugin>
    <groupId>org.lesscss</groupId>
    <artifactId>lesscss-maven-plugin</artifactId>
    <version>1.3.3</version>
    <configuration>
        <sourceDirectory>${project.basedir}/src/main/webapp/styles</sourceDirectory>
        <outputDirectory>${lesscss.outputDirectory}</outputDirectory>      
    </configuration>
    <executions>
        <execution>
            <goals>
                 <goal>compile</goal>
            </goals>
            <phase>process-sources</phase>
        </execution>
    </executions>
</plugin>

来源https://github.com/marceloverdijk/lesscss-maven-plugin/issues/8

使用m2e-wro4j

它承诺:

  

执行wro4j-maven-plugin:在Eclipse增量构建上运行,如果在wro4j-maven-plugin的contextFolder(默认情况下为src / main / webapp)下的css,js,less,json,sass资源上检测到更改

来源https://github.com/jbosstools/m2e-wro4j

答案 1 :(得分:1)

我的实际解决方法的想法是通过&#34; hand&#34;修改target\m2e-wtp中的css文件。

(首先我尝试使用maven-resource-plugin将css文件从target\generated-sources复制到target\m2e-wtp,但由于未知的原因,即使是maven-resource-plugin也没有在target\generated-sources中提交的css文件更新时进行处理。)

所以我提出了这个问题:让lesscss-maven-plugin生成文件两次,一次生成target\generated-sources,第二次生成target\m2e-wtp。当然lesscss-maven-plugin只有一个输出文件夹,因此必须两次运行less:compile目标。 (这有点慢,但它有效。) 因为只在eclipse中需要第二组css文件,所以我将第二次执行添加到配置文件中。

    <profile>
        <id>less-eclipse-m2e-workarround</id>
        <!-- 
            problem: Eclipse is not updating m2e-wtp folder when css files in generated-sources get modified
            workarround: generate the css twice: the normal nonce for generated-sources and a second bunch (only
            for eclipse) directly into m2e-wtp folder
            to enable this profile add the profile-id to: project/properties/maven/active maven profiles
            details: http://stackoverflow.com/questions/23521410/automatic-update-of-generated-css-files-via-m2e
        -->
        <build>
            <plugins>
                <plugin>
                    <groupId>org.lesscss</groupId>
                    <artifactId>lesscss-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>for-eclipse</id>
                            <phase>generate-resources</phase>
                            <goals>
                                <goal>compile</goal>                            
                            </goals>
                            <configuration>
                                <outputDirectory>${project.build.directory}/m2e-wtp/web-resources/styles</outputDirectory>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>