{"id":902,"date":"2014-10-23T14:10:05","date_gmt":"2014-10-23T13:10:05","guid":{"rendered":"http:\/\/oprsteny.cz\/?p=902"},"modified":"2014-10-23T14:10:05","modified_gmt":"2014-10-23T13:10:05","slug":"java-ant-script-to-build-a-project-and-sign-the-output-archive","status":"publish","type":"post","link":"https:\/\/oprsteny.cz\/?p=902","title":{"rendered":"Java &#8211; ANT script to build a project and sign the output archive"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"897\" data-permalink=\"https:\/\/oprsteny.cz\/?attachment_id=897\" data-orig-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/eclipse.png\" data-orig-size=\"44,42\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"Java\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/eclipse.png\" class=\"size-full wp-image-897 alignleft\" src=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/eclipse.png\" alt=\"Java\" width=\"44\" height=\"42\" \/>The <em>ANT Script <\/em>described in this article demonstrates how to build and compile a Java project, put it into signle JAR file and finally sign it using self-signed certificate.<!--more--><\/p>\n<ol>\n<li>Sript starts with source files stored in directory <em>&#8216;src&#8217;, <\/em>compiles them and put the result into another directory called <em>&#8216;bin&#8217;<\/em>.<\/li>\n<li>All compiled files are packed into signle JAR file named [ProjectName]-[CurrentDate][CurrentTime].jar into directory<em> &#8216;dist&#8217;. <\/em><\/li>\n<li>After we have the jar file available we can sign it.<em><br \/>\n<\/em><\/p>\n<ol>\n<li>At first we create new self-signed certificate (this step is optional\/not required in case you already have your own signing certificate)<\/li>\n<li>Next we use our certificate to sign the JAR file we&#8217;ve just generated<\/li>\n<\/ol>\n<\/li>\n<li>The last step is adding a manifest file with attributes you want to include.<\/li>\n<\/ol>\n<p><em>Note:<\/em><br \/>\nTo be able to generate the self-signed certificate and sign the JAR file, you must have the JDK installed on your PC.<\/p>\n<pre lang=\"xml\">&lt;?xml version=\"1.0\"?&gt;\r\n\r\n&lt;project name=\"MyJavaProject\" default=\"dist\" basedir=\".\"&gt;\r\n  &lt;description&gt;\r\n    Build file for JProjects application\r\n  &lt;\/description&gt;\r\n  &lt;!-- Set GLOBAL properties for build --&gt;\r\n  &lt;property name=\"src\" location=\"src\" \/&gt;\r\n  &lt;property name=\"build\" location=\"bin\" \/&gt;\r\n  &lt;property name=\"dist\" location=\"dist\" \/&gt;\r\n\r\n  &lt;property name=\"verisign.key.store\" value=\"NULL\/.keystore\" \/&gt;\r\n  &lt;property name=\"verisign.key.storepass\" value=\"St0r3P@ssw0rd\" \/&gt;\r\n  &lt;property name=\"verisign.key.alias\" value=\"Alice\" \/&gt;\r\n  &lt;property name=\"verisign.key.pass\" value=\"P@ssw0rdF0rAlic3\" \/&gt;\r\n\r\n  &lt;presetdef name=\"javac\"&gt;\r\n    &lt;javac includeantruntime=\"false\" \/&gt;\r\n  &lt;\/presetdef&gt;\r\n\r\n  &lt;target name=\"init\" depends=\"clean\" description=\"Prepare directory structure\"&gt;\r\n    &lt;!-- Create the time stamp --&gt;\r\n    &lt;tstamp \/&gt;\r\n    &lt;!-- Create the build directory structure used by compile --&gt;\r\n    &lt;mkdir dir=\"NULL\" \/&gt;\r\n    &lt;mkdir dir=\"NULL\/lib\" \/&gt;\r\n  &lt;\/target&gt;\r\n  &lt;target name=\"compile\" depends=\"init\" description=\"Compile the source \"&gt;\r\n    &lt;!-- Compile the java code from NULL into NULL --&gt;\r\n    &lt;javac srcdir=\"NULL\" destdir=\"NULL\" \/&gt;\r\n  &lt;\/target&gt;\r\n\r\n  &lt;target name=\"dist\" depends=\"compile\" description=\"Generate the distribution\"&gt;\r\n    &lt;!-- Put everything in NULL into the ${ant.project.name}-NULL-NULL.jar file --&gt;\r\n    &lt;jar \r\n      jarfile=\"NULL\/lib\/${ant.project.name}-NULLNULL.jar\" \r\n      basedir=\"NULL\"&gt;\r\n      &lt;manifest id=\"MANIFEST.MF\"&gt;\r\n        &lt;attribute name=\"Built-By\" value=\"${user.name}\" \/&gt;\r\n        &lt;attribute name=\"Application-Name\" value=\"My Application Name\" \/&gt;\r\n        &lt;attribute name=\"Application-Library-Allowable-Codebase\" value=\"*\" \/&gt;\r\n        &lt;attribute name=\"Caller-Allowable-Codebase\" value=\"*\" \/&gt;\r\n        &lt;attribute name=\"Codebase\" value=\"*\" \/&gt;\r\n        &lt;attribute name=\"Permissions\" value=\"all-permissions\" \/&gt;\r\n        &lt;attribute name=\"Sealed\" value=\"true\" \/&gt;\r\n        &lt;attribute name=\"Trusted-Library\" value=\"true\" \/&gt;\r\n\r\n        &lt;section name=\"UserName.class\"&gt;\r\n          &lt;attribute name=\"Sealed\" value=\"true\" \/&gt;\r\n        &lt;\/section&gt;\r\n      &lt;\/manifest&gt;\r\n    &lt;\/jar&gt;\r\n    &lt;antcall target=\"signjars\" \/&gt;\r\n  &lt;\/target&gt;\r\n\r\n  &lt;target name=\"signjars\"&gt;\r\n    &lt;genkey alias=\"${verisign.key.alias}\" \r\n            verbose=\"true\" \r\n            storepass=\"${verisign.key.storepass}\" \r\n            keypass=\"${verisign.key.pass}\" \r\n            validity=\"365\" \r\n            keystore=\"${verisign.key.store}\"&gt;\r\n      &lt;dname&gt;\r\n        &lt;param name=\"CN\" value=\"Application Outsourcing\" \/&gt;\r\n        &lt;param name=\"OU\" value=\"Application Outsourcing\" \/&gt;\r\n        &lt;param name=\"O\" value=\"IT development company\" \/&gt;\r\n        &lt;param name=\"C\" value=\"US\" \/&gt;\r\n        &lt;param name=\"email\" value=\"alice@example.com\" \/&gt;\r\n      &lt;\/dname&gt;\r\n    &lt;\/genkey&gt;\r\n    &lt;signjar \r\n      tsaurl=\"https:\/\/timestamp.geotrust.com\/tsa\" \r\n      jar=\"NULL\/lib\/${ant.project.name}-NULLNULL.jar\" \r\n      signedjar=\"NULL\/lib\/${ant.project.name}-NULLNULL_signed.jar\" \r\n      alias=\"${verisign.key.alias}\" \r\n      storepass=\"${verisign.key.storepass}\" \r\n      keystore=\"${verisign.key.store}\" \r\n      keypass=\"${verisign.key.pass}\" \/&gt;\r\n  &lt;\/target&gt;\r\n\r\n  &lt;target name=\"clean\" description=\"clean up\"&gt;\r\n    &lt;!-- Delete the NULL and NULL directory trees --&gt;\r\n    &lt;delete dir=\"NULL\" \/&gt;\r\n    &lt;delete dir=\"NULL\" \/&gt;\r\n  &lt;\/target&gt;\r\n&lt;\/project&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The ANT Script described in this article demonstrates how to build and compile a Java project, put it into signle JAR file and finally sign it using self-signed certificate.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"Java - ANT script to build a project and sign the output archive","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[9,10,5],"tags":[346,248,345,444,347,348],"class_list":["post-902","post","type-post","status-publish","format-standard","hentry","category-development","category-java","category-tools","tag-ant","tag-certificate","tag-eclipse","tag-java","tag-jdk","tag-script"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3nYbe-ey","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/902","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=902"}],"version-history":[{"count":3,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/902\/revisions"}],"predecessor-version":[{"id":1181,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/902\/revisions\/1181"}],"wp:attachment":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}