开发者

Custom Maven plugin for encryption

I'm translating an Ant script to Maven 2 and I have this problem: the Ant script use a pretty simple java class to encrypt files this way:

<target name="encrypt">
    <java classname="DESEncrypter">
        <classpath>
            <pathelement path="...class开发者_C百科path for this thing..." />
        </classpath>
        <arg line="fileToEncrypt.properties fileEncrypted.properties" />
    </java>
</target>

This DESEncrypter is a compiled class which source doesn't belong to the project I am converting but is used similarly in other projects. Probably I have to create a maven plugin for this to reuse, but I don't want to do it now. My question is: in which directory do i put the DESEncrypter class and how do i invoke it? Using the exec:java plugin, may be? I don't think the encrypter belong to src, test or resources directories.

Obviously, I don't want to include the encrypter class in the final product, just the encrypted files.


My question is: in which directory do i put the DESEncrypter class and how do i invoke it? Using the exec:java plugin, may be? I don't think the encrypter belong to src, test or resources directories.

A very straightforward solution would be to use the Maven AntRun Plugin. Regarding the location of your encrypter, you could either:

  • put it in a separate module that you could declare as dependency OF the plugin (see this example)
  • keep it in the current module, in the source tree, and configure the Maven JAR Plugin to exclude it using excludes.


The third obvious answer (apart from exec:java and antrun) is GMaven, which lets you execute Groovy code either from an external class or inline from your pom. So if you only need a one-liner, embedding it in your pom is a quick and easy way to implement things (otherwise you should use an external script). BTW: if you don't know groovy: it's basically java with some additional syntax sugaring.

Here's a sample configuration (of course you have to replace the artifact and class you use):

<plugin>
    <groupId>org.codehaus.groovy.maven</groupId>
    <artifactId>gmaven-plugin</artifactId>
    <dependencies>
        <dependency>
            <groupId>your.library.com</groupId>
            <artifactId>your-library</artifactId>
            <version>1.2.3</version>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <phase>process-classes</phase>
            <!-- Or any other phase -->
            <goals>
                <goal>execute</goal>
            </goals>
            <configuration>
                <source><![CDATA[
                import com.encryption.*;
                new Encrypter().encrypt(
                    new File(project.build.outputDirectory, 
                                                    'fileToEncrypt.properties'),
                    new File(project.build.outputDirectory, 
                                                    'encryptedFile.properties')
                )
                ]]></source>
            </configuration>
        </execution>
    </executions>
</plugin>

(By making the encryption artifact a plugin dependency, you keep it out of your deployed dependencies, but this holds true for antrun and exec:java also)


You might want to just use the AntRun plugin, it should let you accomplish anything from Ant with a minimum amount of fuss. You would need a dependency on the class/jar you were using, but by giving it a scope of test, or provided it won't package it in your final product.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜