Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrate yahoo smush.it in maven build for image compression

I want to integrate Yahoo smush.it in maven build to automate the image compression in build itself.

Can anyone help me to do so?

I'm open to other libraries as well. [Back-end is Java.]

like image 732
SHANK Avatar asked Oct 31 '22 23:10

SHANK


1 Answers

Have you considered writing a small Maven plugin to automate this yourself? The plugin API is great, and really simple - you can check it out here. Basically, you would create a plugin project that takes some XML parameters and performs the conversion for you:

@Mojo(name = "compress", defaultPhase = "compile")
public class SmushItCompressMojo extends AbstractMojo {

    @Parameter(property = "images")
    String[] images;

    @Parameter(property = "destination") 
    String destination;

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        // Validate your inputs.
        // For each image file:
            // Compress it using a request to smush.it.
            // Save the compressed image to the destination file.
        // Report any errors/success.
    }
}

Then, in the pom.xml that wishes to use your newly written mojo, use it as follows in the <plugins> tag under <build>:

<plugin>
    <groupId>com.stackoverflow</groupId>
    <artifactId>smush-it-maven-plugin</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <executions>
        <execution>
            <id>compress</id>
            <goals>
                <goal>compress</goal>
            </goal>
            <configuration>
                <images>
                    <image>${project.build.directory}/../images/1.png</image>
                    <image>${project.build.directory}/../images/2.png</image>
                    <image>${project.build.directory}/../images/3.png</image>
                </images>
                <destination>${project.build.directory}/../src/main/resources/compressed/
            </configuration>
        </execution>
    </executions>
</plugin>

Then you can expect the three images to be saved into the compressed resources folder, which will then get packaged up in a later lifecycle stage. Obviously there's a lot of flexibility here about exactly where the images come from and get saved. But the mojo itself is pretty straightforward, and this is exactly how you automate your application-specific tasks to work with Maven.

like image 102
Misha Avatar answered Nov 08 '22 03:11

Misha