Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic update of generated css files via m2e

I'm using the lesscss-maven-plugin to generate different css files to the target directory (target\generated-sources) and then use maven-war-plugin to add this directory as an webResouce. Those files will generate perfectly fine.

However the m2e-plugin (version 1.0.0) won't copy those files in the according web-resources folder (m2e-wtp\web-resources), when they have changed. Only when I run a eclipse "maven/update project" changes will be updated. But I want the changes to take affect automatically, when the files have changed. Here is my pom configuration:

<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>
like image 882
mejoz Avatar asked May 07 '14 15:05

mejoz


2 Answers

There are two options:

Use an m2e specific profile

This is similar to the workaround you found, but a bit cleaner as it activates the profile only on m2e and you use a property to set an alternative value for when you package not using m2e.

You create an m2e specific profile that, when activated, will put the files directly in the m2e-wtp/web-resources/styles directory

<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>

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

Use m2e-wro4j

It promises to:

execute wro4j-maven-plugin:run on Eclipse incremental builds, if a change is detected on css, js, less, json, sass resources under wro4j-maven-plugin's contextFolder (src/main/webapp by default)

Source: https://github.com/jbosstools/m2e-wro4j

like image 180
Boj Avatar answered Nov 02 '22 00:11

Boj


The idea for my actual workaround was to modify the css file in target\m2e-wtp by "hand".

(First I tried to copy the css files from target\generated-sources to target\m2e-wtp with the maven-resource-plugin, but for a unkown reason, even the maven-resource-plugin was not coping when the filed css files in target\generated-sources gets updated.)

So I came up with this soution: let the lesscss-maven-plugin generate the files twice, one bunch to target\generated-sources and the second one to target\m2e-wtp. Of course the lesscss-maven-plugin has only one output folder, so one has to run the less:compile goal twice. (This is a bit slow, but it works.) Because one need the second bunch of css files only in eclipse I have added the second execution to an profile.

    <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>
like image 30
Ralph Avatar answered Nov 02 '22 01:11

Ralph