SignJar (Apache Ant API)
Description: declaration: package: org.apache.tools.ant.taskdefs, class: SignJar

# Class SignJar

java.lang.Object
org.apache.tools.ant.ProjectComponent
org.apache.tools.ant.Task
org.apache.tools.ant.taskdefs.AbstractJarSignerTask
org.apache.tools.ant.taskdefs.SignJar

All Implemented Interfaces:
  `Cloneable`

public class SignJar extends AbstractJarSignerTask
Signs JAR or ZIP files with the javasign command line tool. The tool detailed dependency checking: files are only signed if they are not signed. The `signjar` attribute can point to the file to generate; if this file exists then its modification date is used as a cue as to whether to resign any JAR file. Timestamp signature support is based on Java 8

Since:
  Ant 1.1
See Also:
  - documentation

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `protected File`
  `destDir`
  the output directory when using paths.
  `static final String`
  `ERROR_BAD_MAP`
  error string for unit test verification: "Cannot map source file to anything sensible: "
  `static final String`
  `ERROR_MAPPER_WITHOUT_DEST`
  error string for unit test verification: "The destDir attribute is required if a mapper is set"
  `static final String`
  `ERROR_NO_ALIAS`
  error string for unit test verification: "alias attribute must be set"
  `static final String`
  `ERROR_NO_STOREPASS`
  error string for unit test verification: "storepass attribute must be set"
  `static final String`
  `ERROR_SIGNEDJAR_AND_PATHS`
  error string for unit test verification "You cannot specify the signed JAR when using paths or filesets"
  `static final String`
  `ERROR_TODIR_AND_SIGNEDJAR`
  error string for unit test verification: "\'destdir\' and \'signedjar\' cannot both be set"
  `static final String`
  `ERROR_TOO_MANY_MAPPERS`
  error string for unit test verification: "Too many mappers"
  `protected boolean`
  `internalsf`
  flag for internal sf signing
  `protected boolean`
  `lazy`
  Whether to assume a jar which has an appropriate .SF file in is already signed.
  `protected boolean`
  `sectionsonly`
  sign sections only?
  `protected String`
  `sigfile`
  name to a signature file
  `protected File`
  `signedjar`
  name of a single jar
  `protected String`
  `tsacert`
  alias for the TSA in the keystore
  `protected String`
  `tsaproxyhost`
  Proxy host to be used when connecting to TSA server
  `protected String`
  `tsaproxyport`
  Proxy port to be used when connecting to TSA server
  `protected String`
  `tsaurl`
  URL for a tsa; null implies no tsa support

  ### Fields inherited from class AbstractJarSignerTask

  `alias, ERROR_NO_SOURCE, filesets, jar, JARSIGNER_COMMAND, keypass, keystore, maxMemory, storepass, storetype, strict, verbose`
  Modifier and Type
  Field
  Description
  `protected String`
  `alias`
  The alias of signer.
  `static final String`
  `ERROR_NO_SOURCE`
  error string for unit test verification: "jar must be set through jar attribute or nested filesets"
  `protected Vector<FileSet>`
  `filesets`
  the filesets of the jars to sign
  `protected File`
  `jar`
  The name of the jar file.
  `protected static final String`
  `JARSIGNER_COMMAND`
  name of JDK program we are looking for
  `protected String`
  `keypass`
  password for the key in the store
  `protected String`
  `keystore`
  The url or path of keystore file.
  `protected String`
  `maxMemory`
  The maximum amount of memory to use for Jar signer
  `protected String`
  `storepass`
  password for the store
  `protected String`
  `storetype`
  type of store,-storetype param
  `protected boolean`
  `strict`
  strict checking
  `protected boolean`
  `verbose`
  verbose output

  ### Fields inherited from class Task

  `target, taskName, taskType, wrapper`
  Modifier and Type
  Field
  Description
  `protected Target`
  `target`
  Deprecated.
  since 1.6.x.
  `protected String`
  `taskName`
  Deprecated.
  since 1.6.x.
  `protected String`
  `taskType`
  Deprecated.
  since 1.6.x.
  `protected RuntimeConfigurable`
  `wrapper`
  Deprecated.
  since 1.6.x.

  ### Fields inherited from class ProjectComponent

  `description, location, project`
  Modifier and Type
  Field
  Description
  `protected String`
  `description`
  Deprecated.
  since 1.6.x.
  `protected Location`
  `location`
  Deprecated.
  since 1.6.x.
  `protected Project`
  `project`
  Deprecated.
  since 1.6.x.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `SignJar()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `add(FileNameMapper newMapper)`
  add a mapper to determine file naming policy.
  `void`
  `execute()`
  sign the jar(s)
  `String`
  `getDigestAlg()`
  Digest Algorithm; optional
  `FileNameMapper`
  `getMapper()`
  get the active mapper; may be null
  `String`
  `getSigAlg()`
  Signature Algorithm; optional
  `String`
  `getTsacert()`
  get the -tsacert option
  `String`
  `getTSADigestAlg()`
  TSA Digest Algorithm; optional
  `String`
  `getTsaproxyhost()`
  Get the proxy host to be used when connecting to the TSA url
  `String`
  `getTsaproxyport()`
  Get the proxy host to be used when connecting to the TSA url
  `String`
  `getTsaurl()`
  get the -tsaurl url
  `boolean`
  `isForce()`
  Should the task force signing of a jar even it is already signed?
  `protected boolean`
  `isSigned(File file)`
  test for a file being signed, by looking for a signature in the META-INF directory with our alias/sigfile.
  `protected boolean`
  `isUpToDate(File jarFile, File signedjarFile)`
  Compare a jar file with its corresponding signed jar.
  `void`
  `setDestDir(File destDir)`
  Optionally sets the output directory to be used.
  `void`
  `setDigestAlg(String digestAlg)`
  Digest Algorithm; optional
  `void`
  `setForce(boolean b)`
  Whether to force signing of a jar even it is already signed.
  `void`
  `setInternalsf(boolean internalsf)`
  Flag to include the .SF file inside the signature; optional; default false
  `void`
  `setLazy(boolean lazy)`
  flag to control whether the presence of a signature file means a JAR is signed; optional, default false
  `void`
  `setPreserveLastModified(boolean preserveLastModified)`
  true to indicate that the signed jar modification date remains the same as the original.
  `void`
  `setSectionsonly(boolean sectionsonly)`
  flag to compute hash of entire manifest; optional, default false
  `void`
  `setSigAlg(String sigAlg)`
  Signature Algorithm; optional
  `void`
  `setSigfile(String sigfile)`
  name of .SF/.DSA file; optional
  `void`
  `setSignedjar(File signedjar)`
  name of signed JAR file; optional
  `void`
  `setTsacert(String tsacert)`
  set the alias in the keystore of the TSA to use;
  `void`
  `setTSADigestAlg(String digestAlg)`
  TSA Digest Algorithm; optional
  `void`
  `setTsaproxyhost(String tsaproxyhost)`
  `void`
  `setTsaproxyport(String tsaproxyport)`
  `void`
  `setTsaurl(String tsaurl)`

  ### Methods inherited from class AbstractJarSignerTask

  `addArg, addArgument, addFileset, addSysproperty, addValue, beginExecution, bindToKeystore, createJarSigner, createPath, createUnifiedSourcePath, createUnifiedSources, declareSysProperty, endExecution, getRedirector, hasResources, setAlias, setCommonOptions, setExecutable, setJar, setKeypass, setKeystore, setMaxmemory, setProviderArg, setProviderClass, setProviderName, setStorepass, setStoretype, setStrict, setVerbose`
  Modifier and Type
  Method
  Description
  `void`
  `addArg(Commandline.Argument arg)`
  Adds a nested <arg> element that can be used to specify command line arguments not supported via specific attributes.
  `protected void`
  `addArgument(ExecTask cmd, Commandline.Argument arg)`
  add an argument to a command
  `void`
  `addFileset(FileSet set)`
  Adds a set of files to sign
  `void`
  `addSysproperty(Environment.Variable sysp)`
  Add a system property.
  `protected void`
  `addValue(ExecTask cmd, String value)`
  add a value argument to a command
  `protected void`
  `beginExecution()`
  init processing logic; this is retained through our execution(s)
  `protected void`
  `bindToKeystore(ExecTask cmd)`
  bind to a keystore if the attributes are there
  `protected ExecTask`
  `createJarSigner()`
  create the jarsigner executable task
  `Path`
  `createPath()`
  Adds a path of files to sign.
  `protected Path`
  `createUnifiedSourcePath()`
  clone our path and add all explicitly specified FileSets as well, patch in the jar attribute as a new fileset if it is defined.
  `protected Vector<FileSet>`
  `createUnifiedSources()`
  clone our filesets vector, and patch in the jar attribute as a new fileset, if is defined
  `protected void`
  `declareSysProperty(ExecTask cmd, Environment.Variable property)`
  `protected void`
  `endExecution()`
  any cleanup logic
  `RedirectorElement`
  `getRedirector()`
  get the redirector.
  `protected boolean`
  `hasResources()`
  Has either a path or a fileset been specified?
  `void`
  `setAlias(String alias)`
  the alias to sign under; required
  `protected void`
  `setCommonOptions(ExecTask cmd)`
  these are options common to signing and verifying
  `void`
  `setExecutable(String executable)`
  Sets the actual executable command to invoke, instead of the binary `jarsigner` found in Ant's JDK.
  `void`
  `setJar(File jar)`
  the jar file to sign; required
  `void`
  `setKeypass(String keypass)`
  password for private key (if different); optional
  `void`
  `setKeystore(String keystore)`
  keystore location; required
  `void`
  `setMaxmemory(String max)`
  Set the maximum memory to be used by the jarsigner process
  `void`
  `setProviderArg(String providerArg)`
  Sets the value for the -providerArg command line argument.
  `void`
  `setProviderClass(String providerClass)`
  Sets the value for the -providerClass command line argument.
  `void`
  `setProviderName(String providerName)`
  Sets the value for the -providerName command line argument.
  `void`
  `setStorepass(String storepass)`
  password for keystore integrity; required
  `void`
  `setStoretype(String storetype)`
  keystore type; optional
  `void`
  `setStrict(boolean strict)`
  do strict checking
  `void`
  `setVerbose(boolean verbose)`
  Enable verbose output when signing; optional: default false

  ### Methods inherited from class Task

  `bindToOwner, getOwningTarget, getRuntimeConfigurableWrapper, getTaskName, getTaskType, getWrapper, handleErrorFlush, handleErrorOutput, handleFlush, handleInput, handleOutput, init, isInvalid, log, log, log, log, maybeConfigure, perform, reconfigure, setOwningTarget, setRuntimeConfigurableWrapper, setTaskName, setTaskType`
  Modifier and Type
  Method
  Description
  `final void`
  `bindToOwner(Task owner)`
  Bind a task to another; use this when configuring a newly created task to do work on behalf of another.
  `Target`
  `getOwningTarget()`
  Returns the container target of this task.
  `RuntimeConfigurable`
  `getRuntimeConfigurableWrapper()`
  Returns the wrapper used for runtime configuration.
  `String`
  `getTaskName()`
  Returns the name to use in logging messages.
  `String`
  `getTaskType()`
  Return the type of task.
  `protected RuntimeConfigurable`
  `getWrapper()`
  Return the runtime configurable structure for this task.
  `protected void`
  `handleErrorFlush(String output)`
  Handles an error line by logging it with the WARN priority.
  `protected void`
  `handleErrorOutput(String output)`
  Handles an error output by logging it with the WARN priority.
  `protected void`
  `handleFlush(String output)`
  Handles output by logging it with the INFO priority.
  `protected int`
  `handleInput(byte[] buffer, int offset, int length)`
  Handle an input request by this task.
  `protected void`
  `handleOutput(String output)`
  Handles output by logging it with the INFO priority.
  `void`
  `init()`
  Called by the project to let the task initialize properly.
  `protected final boolean`
  `isInvalid()`
  Has this task been marked invalid?
  `void`
  `log(String msg)`
  Logs a message with the default (INFO) priority.
  `void`
  `log(String msg, int msgLevel)`
  Logs a message with the given priority.
  `void`
  `log(String msg, Throwable t, int msgLevel)`
  Logs a message with the given priority.
  `void`
  `log(Throwable t, int msgLevel)`
  Logs a message with the given priority.
  `void`
  `maybeConfigure()`
  Configures this task - if it hasn't been done already.
  `final void`
  `perform()`
  Performs this task if it's still valid, or gets a replacement version and performs that otherwise.
  `void`
  `reconfigure()`
  Force the task to be reconfigured from its RuntimeConfigurable.
  `void`
  `setOwningTarget(Target target)`
  Sets the target container of this task.
  `void`
  `setRuntimeConfigurableWrapper(RuntimeConfigurable wrapper)`
  Sets the wrapper to be used for runtime configuration.
  `void`
  `setTaskName(String name)`
  Sets the name to use in logging messages.
  `void`
  `setTaskType(String type)`
  Sets the name with which the task has been invoked.

  ### Methods inherited from class ProjectComponent

  `clone, getDescription, getLocation, getProject, setDescription, setLocation, setProject`
  Modifier and Type
  Method
  Description
  `Object`
  `clone()`
  `String`
  `getDescription()`
  Returns the description of the current action.
  `Location`
  `getLocation()`
  Returns the file/location where this task was defined.
  `Project`
  `getProject()`
  Returns the project to which this component belongs.
  `void`
  `setDescription(String desc)`
  Sets a description of the current action.
  `void`
  `setLocation(Location location)`
  Sets the file/location where this task was defined.
  `void`
  `setProject(Project project)`
  Sets the project object of this component.

  ### Methods inherited from class Object

  `equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### ERROR_TODIR_AND_SIGNEDJAR

    public static final String ERROR_TODIR_AND_SIGNEDJAR
    error string for unit test verification: "\'destdir\' and \'signedjar\' cannot both be set"
    See Also:
      - Constant Field Values

  - ### ERROR_TOO_MANY_MAPPERS

    public static final String ERROR_TOO_MANY_MAPPERS
    error string for unit test verification: "Too many mappers"
    See Also:
      - Constant Field Values

  - ### ERROR_SIGNEDJAR_AND_PATHS

    public static final String ERROR_SIGNEDJAR_AND_PATHS
    error string for unit test verification "You cannot specify the signed JAR when using paths or filesets"
    See Also:
      - Constant Field Values

  - ### ERROR_BAD_MAP

    public static final String ERROR_BAD_MAP
    error string for unit test verification: "Cannot map source file to anything sensible: "
    See Also:
      - Constant Field Values

  - ### ERROR_MAPPER_WITHOUT_DEST

    public static final String ERROR_MAPPER_WITHOUT_DEST
    error string for unit test verification: "The destDir attribute is required if a mapper is set"
    See Also:
      - Constant Field Values

  - ### ERROR_NO_ALIAS

    public static final String ERROR_NO_ALIAS
    error string for unit test verification: "alias attribute must be set"
    See Also:
      - Constant Field Values

  - ### ERROR_NO_STOREPASS

    public static final String ERROR_NO_STOREPASS
    error string for unit test verification: "storepass attribute must be set"
    See Also:
      - Constant Field Values

  - ### sigfile

    protected String sigfile
    name to a signature file

  - ### signedjar

    protected File signedjar
    name of a single jar

  - ### internalsf

    protected boolean internalsf
    flag for internal sf signing

  - ### sectionsonly

    protected boolean sectionsonly
    sign sections only?

  - ### lazy

    protected boolean lazy
    Whether to assume a jar which has an appropriate .SF file in is already signed.

  - ### destDir

    protected File destDir
    the output directory when using paths.

  - ### tsaurl

    protected String tsaurl
    URL for a tsa; null implies no tsa support

  - ### tsaproxyhost

    protected String tsaproxyhost
    Proxy host to be used when connecting to TSA server

  - ### tsaproxyport

    protected String tsaproxyport
    Proxy port to be used when connecting to TSA server

  - ### tsacert

    protected String tsacert
    alias for the TSA in the keystore

- ## Constructor Details

  - ### SignJar

    public SignJar()

- ## Method Details

  - ### setSigfile

    public void setSigfile(String sigfile)
    name of .SF/.DSA file; optional
    Parameters:
      `sigfile` - the name of the .SF/.DSA file

  - ### setSignedjar

    public void setSignedjar(File signedjar)
    name of signed JAR file; optional
    Parameters:
      `signedjar` - the name of the signed jar file

  - ### setInternalsf

    public void setInternalsf(boolean internalsf)
    Flag to include the .SF file inside the signature; optional; default false
    Parameters:
      `internalsf` - if true include the .SF file inside the signature

  - ### setSectionsonly

    public void setSectionsonly(boolean sectionsonly)
    flag to compute hash of entire manifest; optional, default false
    Parameters:
      `sectionsonly` - flag to compute hash of entire manifest

  - ### setLazy

    public void setLazy(boolean lazy)
    flag to control whether the presence of a signature file means a JAR is signed; optional, default false
    Parameters:
      `lazy` - flag to control whether the presence of a signature

  - ### setDestDir

    public void setDestDir(File destDir)
    Optionally sets the output directory to be used.
    Parameters:
      `destDir` - the directory in which to place signed jars
    Since:
      Ant 1.7

  - ### add

    public void add(FileNameMapper newMapper)
    add a mapper to determine file naming policy. Only used with toDir processing.
    Parameters:
      `newMapper` - the mapper to add.
    Since:
      Ant 1.7

  - ### getMapper

    public FileNameMapper getMapper()
    get the active mapper; may be null
    Returns:
      mapper or null
    Since:
      Ant 1.7

  - ### getTsaurl

    public String getTsaurl()
    get the -tsaurl url
    Returns:
      url or null
    Since:
      Ant 1.7

  - ### setTsaurl

    public void setTsaurl(String tsaurl)
    Parameters:
      `tsaurl` - the tsa url.
    Since:
      Ant 1.7

  - ### getTsaproxyhost

    public String getTsaproxyhost()
    Get the proxy host to be used when connecting to the TSA url
    Returns:
      url or null
    Since:
      Ant 1.9.5

  - ### setTsaproxyhost

    public void setTsaproxyhost(String tsaproxyhost)
    Parameters:
      `tsaproxyhost` - the proxy host to be used when connecting to the TSA.
    Since:
      Ant 1.9.5

  - ### getTsaproxyport

    public String getTsaproxyport()
    Get the proxy host to be used when connecting to the TSA url
    Returns:
      url or null
    Since:
      Ant 1.9.5

  - ### setTsaproxyport

    public void setTsaproxyport(String tsaproxyport)
    Parameters:
      `tsaproxyport` - the proxy port to be used when connecting to the TSA.
    Since:
      Ant 1.9.5

  - ### getTsacert

    public String getTsacert()
    get the -tsacert option
    Returns:
      a certificate alias or null
    Since:
      Ant 1.7

  - ### setTsacert

    public void setTsacert(String tsacert)
    set the alias in the keystore of the TSA to use;
    Parameters:
      `tsacert` - the cert alias.

  - ### setForce

    public void setForce(boolean b)
    Whether to force signing of a jar even it is already signed.
    Parameters:
      `b` - boolean
    Since:
      Ant 1.8.0

  - ### isForce

    public boolean isForce()
    Should the task force signing of a jar even it is already signed?
    Returns:
      boolean
    Since:
      Ant 1.8.0

  - ### setSigAlg

    public void setSigAlg(String sigAlg)
    Signature Algorithm; optional
    Parameters:
      `sigAlg` - the signature algorithm

  - ### getSigAlg

    public String getSigAlg()
    Signature Algorithm; optional
    Returns:
      String

  - ### setDigestAlg

    public void setDigestAlg(String digestAlg)
    Digest Algorithm; optional
    Parameters:
      `digestAlg` - the digest algorithm

  - ### getDigestAlg

    public String getDigestAlg()
    Digest Algorithm; optional
    Returns:
      String

  - ### setTSADigestAlg

    public void setTSADigestAlg(String digestAlg)
    TSA Digest Algorithm; optional
    Parameters:
      `digestAlg` - the tsa digest algorithm
    Since:
      Ant 1.10.2

  - ### getTSADigestAlg

    public String getTSADigestAlg()
    TSA Digest Algorithm; optional
    Returns:
      String
    Since:
      Ant 1.10.2

  - ### execute

    public void execute() throws BuildException
    sign the jar(s)
    Overrides:
      `execute` in class `Task`
    Throws:
      `BuildException` - on errors

  - ### isUpToDate

    protected boolean isUpToDate(File jarFile, File signedjarFile)

    Compare a jar file with its corresponding signed jar. The logic for this is complex, and best explained in the source itself. Essentially if either file doesn't exist, or the destfile has an out of date timestamp, then the return value is false.

    If we are signing ourself, the check `isSigned(File)` is used to trigger the process.

    Parameters:
      `jarFile` - the unsigned jar file
      `signedjarFile` - the result signed jar file
    Returns:
      true if the signedjarFile is considered up to date

  - ### isSigned

    protected boolean isSigned(File file)
    test for a file being signed, by looking for a signature in the META-INF directory with our alias/sigfile.
    Parameters:
      `file` - the file to be checked
    Returns:
      true if the file is signed
    See Also:
      - `IsSigned.isSigned(File, String)`

  - ### setPreserveLastModified

    public void setPreserveLastModified(boolean preserveLastModified)
    true to indicate that the signed jar modification date remains the same as the original. Defaults to false
    Parameters:
      `preserveLastModified` - if true preserve the last modified time