-->

Rename a generated file in Maven before building W

2019-02-19 04:45发布

问题:

Fingers crossed you can help me!

I am using SmartSprites to combine the PNGs on my landing page into one, so that it will load quicker.

SmartSprite will examine your CSS files, generate a CSS Sprite image, and create a new CSS file which will use this sprite image instead of the originals. What I want to do is replace the original CSS file with the SmartSprite one automatically during my maven WAR build.

So here's what I would like to happen:

  1. SmartSprite scans my CSS file: mystyle.css
  2. SmartSprite create a sprite image, and creates a new mystyle-sprite.css file, which references the new sprite image.
  3. I want to copy mystyle-sprite.css over mystyle.css before the WAR is built, so that I don't have to change any of my references in the JSP files.

Both files are located in the output directory (target/myproj/css). There doesn't seem to be any flag in SmartSprite to override the original file, so I guess it had to be done post-processing.

Below is the maven plugin configuration I'm using for SmartSprite.

        <plugin>
            <groupId>org.carrot2.labs</groupId>
            <artifactId>smartsprites-maven-plugin</artifactId>
            <version>1.0</version>
            <executions>
                <execution>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>spritify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

回答1:

You can use the Maven WAR plugin:

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <configuration>
        <webResources>
            <resource>
                <directory><!-- Your output directory --></directory>
                <targetPath><!-- The target location of the files below --></targetPath>
                <includes>
                    <include><!-- file pattern --></include>
                    <include><!-- file pattern --></include>
                </includes>
            </resource>
        </webResources>
    </configuration>
</plugin>

You should also configure SmartSprites to use a different output directory to preserve the original CSS filename. Try the output-dir-path option with an empty css-file-suffix value.



回答2:

I'm afraid you won't find anything simpler or more elegant than Maven AntRun Plugin with something like this:

<build>
  <plugins>
    <plugin>
      <artifactId>maven-antrun-plugin</artifactId>
      <version>1.7</version>
      <executions>
        <execution>
          <phase>prepare-package</phase>
          <configuration>
            <target>
              <copy file="${project.build.directory}/mystyle-sprite.css"
                tofile="${project.build.directory}/mystyle.css" />
            </target>
          </configuration>
          <goals>
            <goal>run</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>