All Downloads are FREE. Search and download functionalities are using the official Maven repository.

forbiddenapis-3.8.build.xml Maven / Gradle / Ivy

Go to download

Allows to parse Java byte code to find invocations of method/class/field signatures and fail build (Apache Ant, Apache Maven, Gradle, or CLI)

The newest version!
<?xml version="1.0" encoding="UTF-8"?>
<!--
 * (C) Copyright Uwe Schindler (Generics Policeman) and others.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
-->
<project name="Policeman's Forbidden API Checker" default="dist"
  xmlns:artifact="antlib:org.apache.maven.artifact.ant"
  xmlns:ivy="antlib:org.apache.ivy.ant"
  xmlns:au="antlib:org.apache.ant.antunit"
  xmlns:rat="antlib:org.apache.rat.anttasks"
>
  <fail message="Minimum supported ANT version is 1.8.0. Yours: ${ant.version}">
    <condition>
      <not><antversion atleast="1.8.0" /></not>
    </condition>
  </fail>

  <fail message="Minimum supported Java version is 1.7.">
    <condition>
      <not><available classname="java.lang.invoke.MethodHandle"/></not>
    </condition>
  </fail>

  <!-- temporary for cleanup of java.specification.version, to be in format "x.y" -->
  <loadresource property="-cleaned.specification.version">
    <propertyresource name="java.specification.version"/>
    <filterchain>
      <tokenfilter>
        <filetokenizer/>
        <replaceregex pattern="^(\d+\.\d+)(|\..*)$" replace="\1" flags="s"/>
      </tokenfilter>
    </filterchain>
  </loadresource>

  <!-- 
    the propery "ant.java.version" is not always correct, depending on used ANT version.
    E.g. Java 8 is only detected in ANT 1.8.3+.
    
    We want to detect here only a limited set of versions and placed in normalized form in ${build.java.runtime},
    every other version is normalized to "unknown":
    - To define a target to be only run on a specific version, add <equals/> condition to one of the supplied versions.
    - To explicitely exclude specific versions (and unknown ones), add a condition to disallow "unknown" and some versions like "9"!
   -->
  <condition property="build.java.runtime" value="${-cleaned.specification.version}" else="unknown">
    <or>
      <equals arg1="${-cleaned.specification.version}" arg2="1.7"/>
      <equals arg1="${-cleaned.specification.version}" arg2="1.8"/>
      <equals arg1="${-cleaned.specification.version}" arg2="9"/>
      <equals arg1="${-cleaned.specification.version}" arg2="10"/>
      <equals arg1="${-cleaned.specification.version}" arg2="11"/>
      <equals arg1="${-cleaned.specification.version}" arg2="12"/>
      <equals arg1="${-cleaned.specification.version}" arg2="13"/>
      <equals arg1="${-cleaned.specification.version}" arg2="14"/>
      <equals arg1="${-cleaned.specification.version}" arg2="15"/>
      <equals arg1="${-cleaned.specification.version}" arg2="16"/>
      <equals arg1="${-cleaned.specification.version}" arg2="17"/>
      <equals arg1="${-cleaned.specification.version}" arg2="18"/>
      <equals arg1="${-cleaned.specification.version}" arg2="19"/>
      <equals arg1="${-cleaned.specification.version}" arg2="20"/>
      <equals arg1="${-cleaned.specification.version}" arg2="21"/>
      <equals arg1="${-cleaned.specification.version}" arg2="22"/>
      <equals arg1="${-cleaned.specification.version}" arg2="23"/>
    </or>
  </condition>
  
  <property name="build.java.info" value="${java.version} / JVM: ${java.vm.name} / Vendor: ${java.vendor} / OS: ${os.name} (${os.version})"/>
  
  <condition property="is.java7">
    <or>
      <equals arg1="${build.java.runtime}" arg2="1.7"/>
    </or>
  </condition>

  <!-- workaround for https://issues.apache.org/bugzilla/show_bug.cgi?id=53347 -->
  <condition property="build.compiler" value="javac1.7">
    <and>
      <not>
        <isset property="is.java7"/>
      </not>
      <or>
        <antversion exactly="1.8.3" />
        <antversion exactly="1.8.4" />
      </or>
    </and>
  </condition>
  
  <!-- With Java 7, the HTTPS access to Maven central is broken, workaround (no TLS 1.2 support): -->
  <condition property="ivy.maven-central.backup" value="http://insecure.repo1.maven.org/maven2" else="https://repo1.maven.org/maven2">
    <isset property="is.java7"/>
  </condition>

  <condition property="javadoc.doclint.args" value="" else="-Xdoclint:none">
    <isset property="is.java7"/>
  </condition>
  
  <property file="${user.home}/build.properties" />
  <property file="build.properties" />

  <property name="jdk.version" value="1.7"/>
  <property name="min.maven.version" value="3.1.0"/>
  <property name="maven.version" value="3.1.1"/><!-- used for testing -->
  <property name="maven-plugin-plugin.version" value="3.4"/>
  <property name="maven-compiler-plugin.version" value="3.8.1"/>
  
  <!-- extra properties (prefixed by "gradle." added to gradle.properties during testing: -->
  <property name="gradle.org.gradle.warning.mode" value="all"/>
  
  <!-- with fork=false this somehow takes endless to download the internet, so let's fork - and slow down in an other way (startup) -->
  <property name="maven.fork" value="true"/>
  
  <property name="compile.debug" value="true"/>
  <property name="compile.deprecation" value="true"/>
  <property name="build.encoding" value="UTF-8"/>

  <property name="ivy.logging" value="download-only"/>

  <property name="signatures.dir" location="src/main/resources/de/thetaphi/forbiddenapis/signatures"/>
  <property name="groovy-tools.dir" location="src/tools/groovy"/>
  <property name="gradle-test-build-dir" location="build/test-gradle"/>
  <property name="gradle-test-user-dir" location="build/tmp-gradle"/>
  <property name="gradle-publish-build-dir" location="build/gradle-plugin-portal"/>
  <property name="maven-build-dir" location="build/maven"/>
  <property name="documentation-dir" location="build/docs"/>
  <property name="test-results-dir" location="build/test-results"/>
  
  <property name="cli-classname" value="de.thetaphi.forbiddenapis.cli.CliMain"/>

  <!-- define Maven coordinates -->
  <property name="groupId" value="de.thetaphi" />
  <property name="artifactId" value="forbiddenapis" />
  <property name="version" value="3.8"/>
  
  <property name="description" value="Allows to parse Java byte code to find invocations of method/class/field signatures and fail build (Apache Ant, Apache Maven, Gradle, or CLI)"/>

  <!-- define artifacts' name, which follows the convention of Maven -->
  <property name="pom-file" location="dist/${artifactId}-${version}.pom" />
  <property name="jar-file" location="dist/${artifactId}-${version}.jar" />
  <property name="sources-jar-file" location="dist/${artifactId}-${version}-sources.jar" />
  <property name="documentation-file" location="dist/${artifactId}-${version}-docs.zip" />

  <!-- defined maven snapshots and staging repository id and url -->
  <property name="maven-snapshots-repository-id" value="sonatype-nexus-snapshots" />
  <property name="maven-snapshots-repository-url" value="https://oss.sonatype.org/content/repositories/snapshots/" />
  <property name="maven-staging-repository-id" value="sonatype-nexus-staging" />
  <property name="maven-staging-repository-url" value="https://oss.sonatype.org/service/local/staging/deploy/maven2/" />

  <!-- GPG settings -->
  <property name="gpg.exe" value="gpg" />
  <property name="gpg.key" value="E1EE085F" />

  <condition property="tests.supported">
    <or>
      <equals arg1="${build.java.runtime}" arg2="1.7"/>
      <equals arg1="${build.java.runtime}" arg2="1.8"/>
      <equals arg1="${build.java.runtime}" arg2="9"/>
      <equals arg1="${build.java.runtime}" arg2="10"/>
      <equals arg1="${build.java.runtime}" arg2="11"/>
      <equals arg1="${build.java.runtime}" arg2="12"/>
      <equals arg1="${build.java.runtime}" arg2="13"/>
      <equals arg1="${build.java.runtime}" arg2="14"/>
      <equals arg1="${build.java.runtime}" arg2="15"/>
      <equals arg1="${build.java.runtime}" arg2="16"/>
      <equals arg1="${build.java.runtime}" arg2="17"/>
      <equals arg1="${build.java.runtime}" arg2="18"/>
      <equals arg1="${build.java.runtime}" arg2="19"/>
      <!-- Java 20+ for testing is no longer supported as Java 7 support was removed as compile target -->
    </or>
  </condition>

  <condition property="forbiddenchecks.supported">
    <or>
      <isset property="tests.supported"/>
      <equals arg1="${build.java.runtime}" arg2="20"/>
      <equals arg1="${build.java.runtime}" arg2="21"/>
      <equals arg1="${build.java.runtime}" arg2="22"/>
      <equals arg1="${build.java.runtime}" arg2="23"/>
    </or>
  </condition>

  <condition property="gradle.supported">
    <isset property="gradle.exe"/>
  </condition>

  <condition property="isSnapshot">
    <contains string="${version}" substring="-SNAPSHOT"/>
  </condition>

  <filterset id="artifact-props-filterset">
    <filter token="NAME" value="${ant.project.name}"/>
    <filter token="VERSION" value="${version}"/>
    <filter token="GROUPID" value="${groupId}"/>
    <filter token="ARTIFACTID" value="${artifactId}"/>
    <filter token="DESCRIPTION" value="${description}"/>
    <filter token="MAVEN_MIN_VERSION" value="${min.maven.version}"/>
    <filter token="REPO" value="${ivy.maven-central.backup}"/>
  </filterset>

  <property name="ivy.install.version" value="2.5.0" />
  <property name="ivy.jar.dir" location=".ivy" />
  <property name="ivy.jar.file" location="${ivy.jar.dir}/ivy-${ivy.install.version}.jar" />

  <condition property="ivy.available">
    <typefound uri="antlib:org.apache.ivy.ant" name="configure" />
  </condition>

  <target name="-install.ivy" unless="ivy.available">
    <mkdir dir="${ivy.jar.dir}"/>
    <get src="${ivy.maven-central.backup}/org/apache/ivy/ivy/${ivy.install.version}/ivy-${ivy.install.version}.jar" dest="${ivy.jar.file}" skipexisting="true"/>
    <taskdef uri="antlib:org.apache.ivy.ant" classpath="${ivy.jar.file}"/>
  </target>
  
  <target name="-init" depends="-install.ivy">
    <echo level="info" taskname="check-java" message="Detected Java runtime major version: ${build.java.runtime}"/>
    <echo level="info" taskname="check-java" message="Java runtime: ${build.java.info}"/>
    <local name="java.version-message"/>
    <condition property="java.version-message"
      value="Java 7 does not support TLS 1.2, so we have to use the insecure Maven Central mirror. If you are building a release, please run the build at least once with Java 8+ to download all Artifacts using HTTPS. If you don't yet have the Gradle Artifacts downloaded, you have to do this anyways."
      else="You are using Java 8+, so we can use the HTTPS Maven Repository for downloading artifacts."
    >
      <isset property="is.java7"/>
    </condition>
    <echo level="warn" taskname="check-java" message="${java.version-message}"/>
    <ivy:configure file="ivy-settings.xml"/>
    <ivy:resolve log="${ivy.logging}"/>
    <local name="ivy.version-message"/>
    <condition property="ivy.version-message"
      value="The correct Apache Ivy version ${ivy.version} is installed."
      else="You have installed a different Apache Ivy version (${ivy.version}) in your ANT classpath than the expected one (${ivy.install.version}). The build may fail!"
    >
      <equals arg1="${ivy.version}" arg2="${ivy.install.version}"/>
    </condition>
    <echo level="warn" taskname="check-ivy" message="${ivy.version-message}"/>
    <ivy:cachepath pathid="-path.main-build1" conf="build" log="${ivy.logging}"/>
    <ivy:cachepath pathid="-path.main-build2" conf="buildgradle" log="${ivy.logging}"/>
    <path id="path.main-build">
      <!-- order is important as the gradle JAR contains lots of outdated stuff, so should be last! -->
      <path refid="-path.main-build1"/>
      <path refid="-path.main-build2"/>
    </path>
    <ivy:cachepath pathid="path.main-bundle" conf="bundle" log="${ivy.logging}"/>
    <ivy:cachepath pathid="path.test" conf="test" log="${ivy.logging}"/>
    <ivy:cachepath pathid="path.jarjar" conf="jarjar" log="${ivy.logging}"/>
    <path id="path.main-run">
      <path refid="path.main-bundle"/>
      <pathelement path="build/main"/>
    </path>
    <path id="path.tools-build" refid="path.main-run"/>
    <path id="path.tools-run">
      <path refid="path.tools-build"/>
      <pathelement path="build/tools"/>
    </path>
    <path id="path.junit-build">
      <path refid="path.main-run"/>
      <path refid="path.test"/>
    </path>
    <path id="path.junit-run">
      <path refid="path.junit-build"/>
      <pathelement path="build/test"/>
    </path>
    <path id="path.all">
      <path refid="path.main-build"/>
      <path refid="path.test"/>
      <pathelement path="build/main"/>
      <pathelement path="build/tools"/>
      <pathelement path="build/test"/>
    </path>
  </target>

  <target name="-install-tasks" depends="-init" unless="installed.tasks">
    <ivy:cachepath pathid="path.tasks" conf="buildtools" log="${ivy.logging}"/>
    <taskdef name="jarjar" classname="com.tonicsystems.jarjar.JarJarTask" classpathref="path.jarjar"/>
    <taskdef uri="antlib:org.apache.maven.artifact.ant" classpathref="path.tasks"/>
    <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="path.tasks"/>
    <taskdef name="groovyc" classname="org.codehaus.groovy.ant.Groovyc" classpathref="path.tasks"/>
    <typedef uri="antlib:org.apache.rat.anttasks" classpathref="path.tasks"/>
    <artifact:remoteRepository id="mvn.maven-central.backup" url="${ivy.maven-central.backup}"/>
    <!-- Make sure we downloaded Maven, so artifact:mvn can bootstrap itsself -->
    <artifact:dependencies pathId="apache-maven-dependencies" pathType="jar" type="pom,jar">
      <dependency groupId="org.apache.maven" artifactId="apache-maven" version="${maven.version}" type="pom" />
      <remoteRepository refid="mvn.maven-central.backup" />
    </artifact:dependencies>
    <property name="installed.tasks" value="true"/>
  </target>

  <macrodef name="compile">
    <attribute name="module"/>
    <attribute name="classpathref"/>
    <sequential>
      <mkdir dir="build/@{module}"/>
      <copy todir="build/@{module}">
        <fileset dir="src/@{module}/resources" erroronmissingdir="false" />
      </copy>
      <javac includeantruntime="false" srcdir="src/@{module}/java" destdir="build/@{module}"
        source="${jdk.version}" target="${jdk.version}" encoding="${build.encoding}"
        debug="${compile.debug}" deprecation="${compile.deprecation}">
         <classpath refid="@{classpathref}"/>
         <compilerarg value="-Xlint"/>
      </javac>
    </sequential>
  </macrodef>

  <target name="compile" depends="-install-tasks" description="Compile main">
    <compile module="main" classpathref="path.main-build"/>
    <groovyc srcdir="src/main/groovy" destdir="build/main"
      indy="true" targetBytecode="${jdk.version}" encoding="${build.encoding}">
      <classpath>
        <pathelement path="build/main"/>
        <path refid="path.main-build"/>
      </classpath>
    </groovyc>
  </target>

  <target name="compile-tools" depends="compile" description="Compile tools">
    <compile module="tools" classpathref="path.tools-build"/>
  </target>

  <target name="compile-test" depends="compile" description="Compile tests">
    <compile module="test" classpathref="path.junit-build"/>
  </target>

  <target name="clean" description="Clean">
    <delete dir="build"/>
    <delete dir="dist"/>
  </target>

  <target name="remove-local-artifacts" description="Removes the project's artifacts from the local Ivy and Maven repository">
    <delete includeemptydirs="true" verbose="true">
      <fileset dir="${user.home}/.m2/repository" erroronmissingdir="false">
        <include name="de/thetaphi/forbiddenapis/**"/>
        <include name="de/thetaphi/example/**"/>
      </fileset>
      <fileset dir="${user.home}/.ivy2/cache" erroronmissingdir="false">
        <include name="${groupId}/${artifactId}/**"/>
      </fileset>
    </delete>
  </target>

  <target name="-maven-setup" depends="-install-tasks">
    <mkdir dir="${maven-build-dir}"/>
    <copy todir="${maven-build-dir}" encoding="${build.encoding}" outputencoding="${build.encoding}">
      <fileset dir="src/main/maven" includes="*.template"/>
      <filterset refid="artifact-props-filterset"/>
      <globmapper from="*.template" to="*"/>
    </copy>
    <property name="maven.deploy.pom" location="${maven-build-dir}/pom-deploy.xml"/>
    <property name="maven.local.settings" location="${maven-build-dir}/settings.xml"/>
    <artifact:pom id="maven.deploy.pom" file="${maven.deploy.pom}">
      <remoteRepository refid="mvn.maven-central.backup" />
    </artifact:pom>
  </target>
  
  <target name="-maven-uptodate"> 
    <uptodate property="maven-descriptor.uptodate" targetfile="build/main/META-INF/maven/plugin.xml">
      <srcfiles dir="src/main"/>
    </uptodate>
  </target>

  <target name="maven-descriptor" depends="compile,-maven-setup,-install-tasks,-maven-uptodate" unless="maven-descriptor.uptodate" description="Creates the Maven Plugin Descriptor">
    <artifact:mvn pom="${maven-build-dir}/pom-build.xml" mavenVersion="${maven.version}" failonerror="true" fork="${maven.fork}">
      <arg value="-s"/><arg file="${maven.local.settings}"/>
      <arg value="plugin:helpmojo"/>
      <arg value="plugin:descriptor"/>
      <arg value="plugin:report"/>
      <sysproperty key="user.language" value="en"/>
      <sysproperty key="user.country" value="US"/>
      <sysproperty key="java.awt.headless" value="true"/>
      <sysproperty key="injected.src.dir" file="src/main/java"/>
      <sysproperty key="injected.output.dir" file="build/main"/>
      <sysproperty key="injected.build.dir" file="${maven-build-dir}"/>
      <syspropertyset>
        <propertyref regex=".*\.version"/>
        <mapper type="glob" from="*" to="injected.*"/>
      </syspropertyset>
    </artifact:mvn>
    <javac includeantruntime="false" srcdir="${maven-build-dir}/generated-sources/plugin" destdir="build/main"
      source="${jdk.version}" target="${jdk.version}" encoding="${build.encoding}" debug="${compile.debug}"
      deprecation="${compile.deprecation}" classpathref="path.main-build"/>
  </target>
  
  <target name="-get-cli-help" depends="compile" unless="cli-help">
    <local name="-cli-help"/>
    <java classname="${cli-classname}" classpathref="path.main-run"
      failonerror="true" fork="true" outputproperty="-cli-help">
      <arg value="--help"/>
    </java>
    <!-- fix the line endings & command line (cannot be done in java task directly! -->
    <loadresource property="cli-help">
      <string value="${-cli-help}"/>
      <filterchain>
        <fixcrlf eol="lf"/>
        <tokenfilter>
          <replacestring from=" ${cli-classname} " to=" -jar ${artifactId}-${version}.jar "/>
        </tokenfilter>
      </filterchain>
    </loadresource>
  </target>
  
  <target name="documentation" depends="maven-descriptor,-get-cli-help" description="Builds documentation and creates ZIP">
    <mkdir dir="${documentation-dir}"/>
    <copy todir="${documentation-dir}">
      <fileset dir="src/main/docs" excludes="*.xsl" />
    </copy>
    <xslt basedir="${maven-build-dir}/generated-site/xdoc" includes="*.xml" destdir="${documentation-dir}" style="src/main/docs/xdoc2html.xsl" force="false">
      <mapper type="glob" from="*.xml" to="*.html"/>
    </xslt>
    <mkdir dir="${documentation-dir}/tmp"/>
    <javadoc destdir="${documentation-dir}/tmp" locale="en_US" encoding="UTF-8" access="public" classpathref="path.all" group="false" nodeprecatedlist="true"
      use="false" notree="true" noindex="true" nohelp="true" nonavbar="true" charset="UTF-8" docencoding="UTF-8" source="${jdk.version}">
      <fileset dir="src/main/java" includes="de/thetaphi/forbiddenapis/gradle/CheckForbiddenApis*.java"/>
      <arg line="${javadoc.doclint.args}"/>
    </javadoc>
    <copy todir="${documentation-dir}">
      <fileset dir="${documentation-dir}/tmp/de/thetaphi/forbiddenapis/gradle" includes="CheckForbiddenApis*.html" />
      <mapper type="glob" from="CheckForbiddenApis*" to="gradleApi*"/>
      <filterset refid="artifact-props-filterset"/>
      <filterchain>
        <tokenfilter>
          <filetokenizer/>
          <replaceregex pattern="\Q..\E.*?\Q/de/thetaphi/forbiddenapis/gradle/\E" replace="" flags="gi"/>
          <replaceregex pattern="CheckForbiddenApis(\w*?\.html)" replace="gradleApi\1" flags="gi"/>
          <replaceregex pattern="&lt;(img|link)\s.*?&gt;" replace="" flags="gi"/>
          <replaceregex pattern="&lt;(no|)script.*?\/(no|)script&gt;" replace="" flags="gis"/>
          <replaceregex pattern="&lt;hr&gt;\s*&lt;hr&gt;" replace="" flags="gis"/>
          <!-- Java 7+ style workaround: -->
          <replaceregex pattern="\Q&lt;/head&gt;\E" replace="&lt;link rel=&quot;stylesheet&quot; href=&quot;classic-stylesheet.css&quot;&gt;\0" flags="i"/>
        </tokenfilter>
      </filterchain>
    </copy>
    <delete dir="${documentation-dir}/tmp"/>
    <xslt in="build/main/META-INF/maven/${groupId}/${artifactId}/plugin-help.xml" out="${documentation-dir}/index.html" style="src/main/docs/docindex.xsl" force="false">
      <param name="clihelp" expression="${cli-help}"/>
    </xslt>
    <mkdir dir="dist"/>
    <zip destfile="${documentation-file}" encoding="${build.encoding}">
      <zipfileset dir="${documentation-dir}" prefix="${artifactId}-${version}"/>
    </zip>
  </target>

  <target name="jar" depends="compile,-install-tasks,maven-descriptor" description="Create the binary JAR">
    <ivy:cachefileset setid="fileset.bundle" conf="bundle" log="${ivy.logging}"/>
    <mkdir dir="dist"/>
    <jarjar destfile="${jar-file}">
      <zipfileset dir="build/main" excludes="org/gradle/**"/>
      <restrict>
        <name name="**/*.class" handledirsep="true"/>
        <not>
          <name name="module-info.class" handledirsep="true"/>
        </not>
        <archives>
          <zips><fileset refid="fileset.bundle"/></zips>
        </archives>
      </restrict>
      <keep pattern="de.thetaphi.forbiddenapis.**"/>
      <rule pattern="org.objectweb.asm.**" result="de.thetaphi.forbiddenapis.asm.@1"/>
      <rule pattern="org.apache.commons.**" result="de.thetaphi.forbiddenapis.commons.@1"/>
      <rule pattern="org.codehaus.plexus.**" result="de.thetaphi.forbiddenapis.plexus.@1"/>
      <manifest>
        <attribute name="Implementation-Vendor" value="${groupId}"/>
        <attribute name="Implementation-Title" value="${artifactId}"/>
        <attribute name="Implementation-Version" value="${version}"/>
        <attribute name="Main-Class" value="${cli-classname}"/>
      </manifest>
      <metainf dir="." includes="LICENSE.txt,NOTICE.txt,bundled-licenses/**"/>
    </jarjar>
  </target>

  <target name="jar-src" description="Create source distribution">
    <mkdir dir="dist"/>
    <zip destfile="${sources-jar-file}" encoding="${build.encoding}">
      <zipfileset dir="." excludes="build/**,dist/**" prefix="${artifactId}-${version}"/>
    </zip>
  </target>

  <target name="install-maven-artifacts" depends="-install-tasks,jar,jar-src,documentation" description="Installs the Maven artifacts in ~/.m2">
    <artifact:install file="${jar-file}">
      <pom refid="maven.deploy.pom"/>
      <attach file="${sources-jar-file}" classifier="sources"/>
      <attach file="${documentation-file}" classifier="docs" type="zip"/>
    </artifact:install>
  </target>

  <target name="dist" depends="rat-sources,install-maven-artifacts" description="Create the distribution">
    <copy file="${maven.deploy.pom}" tofile="${pom-file}"/>
  </target>

  <target name="stage" depends="-stage.snapshots,-stage.release" description="Create the distribution"/>
  
  <target name="-stage.snapshots" depends="dist" if="isSnapshot">
    <artifact:deploy file="${jar-file}">
      <pom refid="maven.deploy.pom"/>
      <attach file="${sources-jar-file}" classifier="sources"/>
      <attach file="${documentation-file}" classifier="docs" type="zip"/>
    </artifact:deploy>
  </target>
  
  <target name="sign-artifacts" depends="dist" unless="isSnapshot">
    <delete failonerror="false" dir="dist" includes="**/*.asc"/>
    <local name="gpg.passphrase"/>

    <input message="Enter GPG keystore password: &gt;" addproperty="gpg.passphrase">
      <handler classname="org.apache.tools.ant.input.SecureInputHandler" />
    </input>

    <apply executable="${gpg.exe}" inputstring="${gpg.passphrase}"
           dest="dist" type="file" maxparallel="1" verbose="yes">
      <arg value="--passphrase-fd"/>
      <arg value="0"/>
      <arg value="--batch"/>
      <arg value="--armor"/>
      <arg value="--default-key"/>
      <arg value="${gpg.key}"/>
      <arg value="--output"/>
      <targetfile/>
      <arg value="--detach-sig"/>
      <srcfile/>
      <fileset dir="dist">
        <include name="**/*.jar"/>
        <include name="**/*.zip"/>
        <include name="**/*.pom"/>
      </fileset>
      <globmapper from="*" to="*.asc"/>
    </apply>
  </target>

  <target name="-stage.release" depends="sign-artifacts" unless="isSnapshot">
    <artifact:deploy file="${jar-file}">
      <pom refid="maven.deploy.pom"/>
      <attach file="${sources-jar-file}" classifier="sources"/>
      <attach file="${documentation-file}" classifier="docs" type="zip"/>
      <!-- signatures: -->
      <attach file="${pom-file}.asc" type="pom.asc"/>
      <attach file="${jar-file}.asc" type="jar.asc"/>
      <attach file="${sources-jar-file}.asc" classifier="sources" type="jar.asc"/>
      <attach file="${documentation-file}.asc" classifier="docs" type="zip.asc"/>
    </artifact:deploy>
  </target>

  <target name="test" depends="-check-myself,-check-bundled-signatures,test-junit,-test-antunit,test-gradle,-runtime-unsupported" description="Run tests"/>

  <target name="check-bundled-signatures" depends="-check-bundled-signatures,-forbiddenchecks-unsupported"
    description="Checks bundled signatures file corresponding to the current JVM. Run after every update!"/>
    
  <target name="-install-forbiddenapi-task" depends="compile" unless="installed.forbiddenapi-task">
    <taskdef name="forbiddenapis" classname="de.thetaphi.forbiddenapis.ant.AntTask" classpathref="path.main-run"/>
    <property name="installed.forbiddenapi-task" value="true"/>
  </target>
  
  <target name="-check-myself" depends="compile,compile-tools,compile-test,-install-forbiddenapi-task">
    <forbiddenapis failOnUnsupportedJava="false" targetVersion="${jdk.version}">
      <classpath refid="path.all"/>
      <fileset dir="build/main"/>
      <fileset dir="build/tools"/>
      <fileset dir="build/test"/>
      <signatures>
        <bundled name="jdk-unsafe"/>
        <bundled name="jdk-deprecated"/>
        <bundled name="jdk-non-portable"/>
        <bundled name="jdk-system-out"/>
        <bundled name="jdk-reflection"/>
        <file file="src/tools/signatures/mysignatures.txt"/>
      </signatures>
    </forbiddenapis>
  </target>

  <target name="-check-bundled-signatures" depends="-install-forbiddenapi-task" if="forbiddenchecks.supported">
    <forbiddenapis failOnUnsupportedJava="true" ignoreEmptyFileset="true" targetVersion="${build.java.runtime}">
      <signatures>
        <bundled name="jdk-unsafe"/>
        <bundled name="jdk-deprecated"/>
        <bundled name="jdk-internal"/>
        <bundled name="jdk-system-out"/>
        <bundled name="jdk-reflection"/>
      </signatures>
    </forbiddenapis>
  </target>

  <target name="-install-antunit" unless="installed.antunit">
    <taskdef uri="antlib:org.apache.ant.antunit" classpathref="path.test"/>
    <taskdef uri="antlib:net.sf.antcontrib" classpathref="path.test"/>
    <property name="installed.antunit" value="true"/>
  </target>
  
  <target name="-antunit-fake-pom">
    <mkdir dir="build/test-maven"/>
    <local name="path.main-build.xslstr"/>
    <pathconvert property="path.main-build.xslstr" pathsep="|" refid="path.main-build"/>
    <xslt in="src/test/antunit/dummy.xml" out="build/test-maven/fake-pom.xml"
      style="src/test/antunit/pom-generator.xsl" force="true"
    >
      <outputproperty name="indent" value="yes"/>
      <param name="jarfiles" expression="${path.main-build.xslstr}"/>
    </xslt>
    <property name="antunit.fake-pom" location="build/test-maven/fake-pom.xml"/>
  </target>

  <target name="test-antunit" depends="-test-antunit,-runtime-unsupported" description="Runs Antunit tests"/>
  
  <target name="-test-antunit" depends="compile,compile-tools,compile-test,install-maven-artifacts,-install-forbiddenapi-task,-install-antunit,-antunit-fake-pom" if="tests.supported">
    <mkdir dir="${test-results-dir}"/>
    <property name="antunit.project.rootdir" location="."/>
    <property name="antunit.main.classes" location="build/main"/>
    <property name="antunit.tools.classes" location="build/tools"/>
    <au:antunit>
      <fileset dir="src/test/antunit" includes="Test*.xml"/>
      <au:plainlistener/>
      <au:xmllistener toDir="${test-results-dir}" logLevel="info"/>
      <propertyset>
        <propertyref prefix="antunit."/>
        <propertyref name="jar-file"/>
        <propertyref regex=".*version"/>
        <propertyref name="groupId"/>
        <propertyref name="artifactId"/>
        <propertyref name="ivy.logging"/>
        <propertyref name="ivy.maven-central.backup"/>
        <propertyref name="maven.local.settings"/>
        <propertyref name="maven.fork"/>
      </propertyset>
      <referenceset>
        <reference regex="path\..*"/>
      </referenceset>
    </au:antunit>
  </target>
  
  <target name="test-junit" depends="compile-test" description="Runs Junit tests">
    <mkdir dir="${test-results-dir}"/>
    <junit haltonfailure="yes" fork="yes" forkmode="once">
      <classpath refid="path.junit-run"/>
      <formatter type="brief" usefile="false" />
      <formatter type="xml"/>
      <assertions>
        <enable package="de.thetaphi.forbiddenapis"/>
      </assertions>
      <batchtest todir="${test-results-dir}">
        <fileset dir="src/test/java" includes="**/*Test.java"/>
      </batchtest>
    </junit>
  </target>
  
  <target name="test-gradle" depends="compile,compile-test,jar,-gradle-unsupported" if="gradle.supported" description="Runs a basic Gradle project to check ourselves">
    <mkdir dir="${gradle-test-build-dir}"/>
    <mkdir dir="${gradle-test-user-dir}"/>
    <copy todir="${gradle-test-build-dir}">
      <fileset dir="src/test/gradle"/>
    </copy>
    <pathconvert property="gradle.forbiddenClasspath" refid="path.main-build"/>
    <pathconvert property="gradle.forbiddenTestClasspath" refid="path.junit-build"/>
    <property name="gradle.forbiddenRootDir" location="."/>
    <property name="gradle.forbiddenSourceCompatibility" value="${jdk.version}"/>
    <echoproperties destfile="${gradle-test-build-dir}/gradle.properties" failonerror="true">
      <propertyset>
        <propertyref prefix="gradle."/>
        <globmapper from="gradle.*" to="*"/>
      </propertyset>
    </echoproperties>
    <exec executable="${gradle.exe}" dir="${gradle-test-build-dir}" searchpath="true" vmlauncher="false" failonerror="true" failifexecutionfails="true" taskname="gradle">
      <arg value="--rerun-tasks"/>
      <arg value="--info"/>
      <arg value="--stacktrace"/>
      <arg value="--no-daemon"/>
      <arg value="--gradle-user-home"/>
      <arg file="${gradle-test-user-dir}"/>
      <arg value="forbiddenApis"/>
    </exec>
  </target>
  
  <target name="rat-sources" depends="-install-tasks" description="Checks licenses of all source files">
    <mkdir dir="build"/>
    <tempfile property="rat.sources.logfile" prefix="rat" destdir="build"/>
    <rat:report reportFile="${rat.sources.logfile}" addDefaultLicenseMatchers="true">
      <fileset dir=".">
        <include name="*"/>
        <include name="src/**"/>
        <exclude name="README.md"/>
        <exclude name="**/jdk-deprecated-*.txt"/>
        <exclude name="**/jdk-internal-*.txt"/>
        <exclude name="**/*.iml"/>
      	<exclude name="**/.*"/>
      </fileset>
    </rat:report>
    <!-- now print the output, for review -->
    <loadfile property="rat.output" srcFile="${rat.sources.logfile}"/>
    <echo taskname="rat">${rat.output}</echo>
    <delete>
      <fileset file="${rat.sources.logfile}">
        <and>
          <containsregexp expression="^0 Unknown Licenses"/>
          <not><containsregexp expression="^\s+!"/></not>
        </and>
      </fileset>
    </delete>
    <!-- fail if we didnt find the pattern -->
    <fail message="Rat problems were found!">
      <condition><available file="${rat.sources.logfile}"/></condition>
    </fail>
  </target>

  <target name="-gradle-unsupported" unless="gradle.supported">
    <echo level="warn" message="Cannot run Gradle test project because runtime is unsupported or 'gradle.exe' property undefined."/>
  </target>
  
  <target name="-runtime-unsupported" unless="tests.supported">
    <echo level="warn" message="Test suite / bundled signatures checker was not executed, because the used JVM version is unsupported: ${build.java.info}"/>
  </target>
  
  <target name="-forbiddenchecks-unsupported" unless="forbiddenchecks.supported">
    <echo level="warn" message="Bundled signatures checker was not executed, because the used JVM version is unsupported: ${build.java.info}"/>
  </target>
  
  <target name="generate-deprecated" depends="compile-tools,-install-tasks" description="Generates deprecated API signature lists for current JDK">
    <mkdir dir="${signatures.dir}"/>
    <property name="deprecated.output.file" location="${signatures.dir}/jdk-deprecated-${build.java.runtime}.txt"/>
    <groovy classpathref="path.tools-run" src="${groovy-tools.dir}/generate-deprecated.groovy"/>
  </target>

  <target name="generate-internal" depends="compile-tools" description="Generates internal API signature lists for current JDK">
    <mkdir dir="${signatures.dir}"/>
    <property name="internal.output.file" location="${signatures.dir}/jdk-internal-${build.java.runtime}.txt"/>
    <java classpathref="path.tools-run" classname="de.thetaphi.forbiddenapis.InternalApiGen" fork="true">
      <arg value="${build.java.runtime}"/>
      <arg file="${internal.output.file}"/>
    </java>
  </target>

  <target name="-generate-test-classes-init">
    <condition property="-gen.sunmisc">
      <available classname="sun.misc.BASE64Encoder"/>
    </condition>
    <condition property="-gen.jdk8">
      <hasmethod classname="java.util.Collections" method="emptySortedSet"/>
    </condition>
  </target>
  
  <target name="-generate-test-classes-sunmisc" if="-gen.sunmisc">
    <echo level="info" message="Generating test classes using sun.misc internal APIs (needs Oracle Java):"/>
    <delete dir="src/test/antunit" includes="Oracle*.class"/>
    <javac includeantruntime="false" srcdir="src/test/antunit" destdir="src/test/antunit" includes="Oracle*.java"
      nowarn="true" source="1.7" target="1.7" debug="true" deprecation="false" encoding="${build.encoding}">
      <!-- This disables the symbol file used to detect internal APIs while compiling: -->
      <compilerarg value="-XDignore.symbol.file=true"/>
    </javac>
  </target>
  
  <target name="-generate-test-classes-jdk7">
    <echo level="info" message="Generating test classes for Java 7:"/>
    <delete dir="src/test/antunit" includes="Java7*.class"/>
    <javac includeantruntime="false" srcdir="src/test/antunit" destdir="src/test/antunit" includes="Java7*.java"
      nowarn="true" source="1.7" target="1.7" debug="true" deprecation="false" encoding="${build.encoding}"/>
  </target>
  
  <target name="-generate-test-classes-jdk8" if="-gen.jdk8">
    <echo level="info" message="Generating test classes for Java 8:"/>
    <delete dir="src/test/antunit" includes="Java8*.class"/>
    <javac includeantruntime="false" srcdir="src/test/antunit" destdir="src/test/antunit" includes="Java8*.java"
      nowarn="true" source="1.8" target="1.8" debug="true" deprecation="false" encoding="${build.encoding}"/>
  </target>
  
  <target name="generate-test-classes" depends="-generate-test-classes-init,-generate-test-classes-sunmisc,-generate-test-classes-jdk7,-generate-test-classes-jdk8"
    description="Regenerates .class files used by tests if the current JDK version supports it"/>
  
  <target name="show-help-mojo" depends="install-maven-artifacts" description="Shows help about mojo usage">
    <artifact:mvn mavenVersion="${maven.version}" failonerror="true" fork="${maven.fork}" taskname="help">
      <arg value="-s"/><arg file="${maven.local.settings}"/>
      <arg value="${groupId}:${artifactId}:${version}:help"/>
      <sysproperty key="detail" value="true"/>
      <sysproperty key="java.awt.headless" value="true"/>
    </artifact:mvn>
  </target>

  <target name="publish-gradle-plugin" description="Publish artifacts (from Maven repo) to Gradle Plugin Portal (pass -Dversion=x.y)">
    <fail unless="gradle.supported" message="You need to enable Gradle support with -Dgradle.exe=/path/to/gradle"/>
    <fail if="isSnapshot" message="You can only publish release versions to Gradle Plugin Portal"/>
    <!-- prepare the Gradle build file (in new empty directory): -->
    <delete failonerror="false" dir="${gradle-publish-build-dir}"/>
    <mkdir dir="${gradle-publish-build-dir}"/>
    <copy todir="${gradle-publish-build-dir}" encoding="${build.encoding}" outputencoding="${build.encoding}">
      <fileset dir="src/tools/gradle-plugin-portal" includes="*.template"/>
      <filterset refid="artifact-props-filterset"/>
      <globmapper from="*.template" to="*"/>
    </copy>
    <!-- Exceute Gradle and execute task 'publishPlugins': -->
    <exec executable="${gradle.exe}" dir="${gradle-publish-build-dir}" searchpath="true" vmlauncher="false" failonerror="true" failifexecutionfails="true" taskname="gradle">
      <arg value="--no-daemon"/>
      <arg value="publishPlugins"/>
    </exec>
  </target>
  
  <target name="jenkins" depends="clean,dist,test,documentation,-stage.snapshots" description="Runs Jenkins Nightly"/>
</project>




© 2015 - 2025 Weber Informatics LLC | Privacy Policy