You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Daniele Development-ML <da...@googlemail.com> on 2009/07/23 19:47:19 UTC

Programmatically invoking Ant libraries to sign jar files - Example

Hello everybody,
some time ago I emailed to ask details/examples on how
to programmatically invoke Ant libraries to sign a jar file. There were
contrasted opinion on whether Ant libraries could be invoked directly and I
was invited to post an example when (and if) I was successful in this.

With a considerable delay (apologies for this):

Please find below the code - hopefully it will be useful to somebody else.

private static void sign(File file) {
        String sigFile = System.getProperty("user.home") +
Paths.SIGNATURE_FILE_PATH;

        Project project = new Project();
        Target target = new Target();
        SignJar sj = new SignJar();
        target.addTask(sj);

        project.addTarget("sign_jar", target);

        sj.setKeystore(sigFile);
        sj.setAlias("alias");

        sj.setKeypass("password");
        sj.setStorepass("keyStorePassword");

        sj.setProject(project);
        sj.setVerbose(true);
        sj.setJar(file);

        project.executeTarget("sign_jar");
    }


Daniele

Re: Programmatically invoking Ant libraries to sign jar files - Example

Posted by Stefan Bodewig <bo...@apache.org>.
On 2009-07-23, Daniele Development-ML <da...@googlemail.com> wrote:

> There were contrasted opinion on whether Ant libraries could be
> invoked directly and I was invited to post an example when (and if)
> I was successful in this.

> With a considerable delay (apologies for this):

> Please find below the code - hopefully it will be useful to somebody else.

Thank you for sharing.

I don't think you need the target at all, you could probably get away
with creating project and task instances, wire them up and
invoke execute() on the task instance.

Something like (untested)

private static void sign(File file) {
    String sigFile = System.getProperty("user.home") +
           Paths.SIGNATURE_FILE_PATH;

    Project project = new Project();
    SignJar sj = new SignJar();
    sj.setProject(project);
    // probably not needed fo SignJar, but it is part of Ant's lifecycle
    sj.init();

    sj.setKeystore(sigFile);
    sj.setAlias("alias");
    sj.setKeypass("password");
    sj.setStorepass("keyStorePassword");
    sj.setVerbose(true);
    sj.setJar(file);

    sj.execute();
}

Stefan

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org