2010-05-20 23 views

Odpowiedz

5

Alternatywą byłoby oprzeć swoją kompilację na scenariuszu maven.
Maven ma zaproponować jarsigner:verify plugin

Jeśli to nie jest poprawnym możliwość, nadal można używać Exec Ant task bezpośrednio wywołać komendę jarsigner. Jeśli kod powrotu jest poprawnie ustawiony, można dodać atrybut failonerror (Zatrzymaj proces kompilacji, jeśli polecenie kończy się z kodem powrotu innym niż 0).

+0

@martin: dziękuję za edycji (typo i link) – VonC

3

Warunki przeciwmgielne oferują "issigned".

"Sprawdź, czy plik jarfile jest podpisany.Jeśli nazwa podpisu zostanie przekazana, plik jest sprawdzany pod kątem obecności tego konkretnego podpisu, w przeciwnym razie plik jest sprawdzany pod kątem istnienia jakiegokolwiek podpisu.Nie wykonuje on rygorystycznego podpisu sprawdzanie poprawności, szuka tylko obecności podpisu: Ten warunek został dodany w Apache Ant 1.7. "

Od Ant conditions

4

Poniższy kod Ant mogą być wykorzystane do weryfikacji podpisów JAR-plików. Skrypt ulegnie niepowodzeniu natychmiast po napotkaniu pliku JAR, w którym podpis jest nieprawidłowy lub jego brakuje.

Należy pamiętać, że ant-contrib jest wymagany do zadania dla.

<!-- Macro to verify whether or not a JAR file is signed --> 
<macrodef name="verify-signatures"> 
    <attribute name="filesetref" /> 
    <sequential> 
     <for param="file"> 
      <path> 
       <fileset refid="@{filesetref}" /> 
      </path> 
      <sequential> 
       <echo message="Verifying signature on file: @{file}" /> 
       <exec executable="jarsigner" failonerror="true"> 
        <arg value="-verify" /> 
        <arg value="@{file}" /> 
       </exec> 
       <fail message="@{file} must be signed"> 
        <condition> 
         <not> 
          <issigned file="@{file}" /> 
         </not> 
        </condition> 
       </fail> 
      </sequential> 
     </for> 
    </sequential> 
</macrodef> 

<!-- Define the list of files to check --> 
<fileset dir="p2repo" id="jarfiles"> 
    <include name="**/*.jar" /> 
</fileset> 

<!-- Verify signatures --> 
<verify-signatures filesetref="jarfiles" /> 
1

Na podstawie odpowiedzi @ torkildr.

Możliwe jest utworzenie ścieżki zagnieżdżonej makra lub zestawu plików do ant-contrib for task.

<target name="verify-artifacts" description="Just an example of usage"> 
    <verify-artifacts> 
     <fileset dir="${project.ear.dir}" includes="*.*ar"/> 
    </verify-artifacts> 
</target> 

<macrodef name="verify-artifacts"> 
    <element name="artifact-path" implicit="true"/> 
    <sequential> 
     <for param="file"> 
      <artifact-path/> 
      <sequential> 
       <verify-artifact file="@{file}"/> 
      </sequential> 
     </for> 
    </sequential> 
</macrodef> 

<macrodef name="verify-artifact"> 
    <attribute name="file"/> 
    <attribute name="alias" default="${artifact.sign.keystore.alias}"/> 
    <attribute name="keystore" default="${artifact.sign.keystore.path}"/> 
    <attribute name="password" default="${artifact.sign.keystore.password}"/> 
    <sequential> 
     <if> 
      <istrue value="${artifact.sign.enabled}"/> 
      <then> 
       <echo message="Trying to verify @{file} with alias @{alias} from @{keystore}"/> 
       <required-macro-param value="@{alias}" prop="artifact.sign.keystore.alias"/> 
       <required-macro-param value="@{keystore}" prop="artifact.sign.keystore.path"/> 
       <required-macro-param value="@{password}" prop="artifact.sign.keystore.password"/> 
       <fail message="Keystore path '@{keystore}' not found"> 
        <condition> 
         <not><available file="@{keystore}" type="file"/></not> 
        </condition> 
       </fail> 
       <fail message="Artifact '@{file}' not found"> 
        <condition> 
         <not><available file="@{file}" type="file"/></not> 
        </condition> 
       </fail> 
       <!-- jarsigner -verify -keystore @{keystore} -storepass @{password} @{file} @{alias} --> 
       <exec executable="jarsigner" failonerror="true"> 
        <arg value="-verify"/> 
        <arg value="-keystore"/> 
        <arg value="@{keystore}"/> 
        <arg value="-storepass"/> 
        <arg value="@{password}"/> 
        <arg value="@{file}"/> 
        <arg value="@{alias}"/> 
       </exec> 
      </then> 
     </if> 
    </sequential> 
</macrodef> 

<macrodef name="required-macro-param"> 
    <attribute name="prop"/> 
    <attribute name="value"/> 
    <sequential> 
     <!--<echo message="@{value}"/>--> 
     <fail message="You must set property '@{prop}'"> 
      <condition> 
       <and> 
        <or> 
         <equals arg1="@{value}" arg2=""/> 
         <matches string="@{value}" pattern="^\$\{.*?\}$"/> 
        </or> 
        <!--<not><isset property="@{prop}"/></not>--> 
       </and> 
      </condition> 
     </fail> 
    </sequential> 
</macrodef> 

<macrodef name="sign-artifact"> 
    <attribute name="file"/> 
    <attribute name="alias" default="${artifact.sign.keystore.alias}"/> 
    <attribute name="keystore" default="${artifact.sign.keystore.path}"/> 
    <attribute name="password" default="${artifact.sign.keystore.password}"/> 
    <sequential> 
     <if> 
      <istrue value="${artifact.sign.enabled}"/> 
      <then> 
       <echo message="Trying to sign @{file} with alias @{alias} from @{keystore}"/> 
       <required-macro-param value="@{alias}" prop="artifact.sign.keystore.alias"/> 
       <required-macro-param value="@{keystore}" prop="artifact.sign.keystore.path"/> 
       <required-macro-param value="@{password}" prop="artifact.sign.keystore.password"/> 
       <fail message="Keystore path '@{keystore}' not found"> 
        <condition> 
         <not><available file="@{keystore}" type="file"/></not> 
        </condition> 
       </fail> 
       <fail message="Artifact '@{file}' not found"> 
        <condition> 
         <not><available file="@{file}" type="file"/></not> 
        </condition> 
       </fail> 
       <signjar jar="@{file}" alias="@{alias}" keystore="@{keystore}" storepass="@{password}"/> 
       <fail message="Signature check failed"> 
        <condition> 
         <not><issigned file="@{file}" name="@{alias}"/></not> 
        </condition> 
       </fail> 
      </then> 
     </if> 
    </sequential> 
</macrodef> 

<macrodef name="sign-artifacts"> 
    <element name="artifact-path" implicit="true"/> 
    <sequential> 
     <for param="file"> 
      <artifact-path/> 
      <sequential> 
       <sign-artifact file="@{file}"/> 
      </sequential> 
     </for> 
    </sequential> 
</macrodef> 

<property name="artifact.sign.enabled" value="true"/> 
<property name="artifact.sign.keystore.alias" value="alias"/> 
<property name="artifact.sign.keystore.path" value="keystore.jks"/> 
<property name="artifact.sign.keystore.password" value="pwd"/> 
+0

Wolę tę odpowiedź, ponieważ faktycznie sprawdza, czy są podpisane słoików z określonym kluczy. Miałem problemy z pierwszym skryptem, ponieważ sprawdzono tylko, czy zostały podpisane i problemy z jarami z innymi sygnatariuszami później. – javydreamercsw

1

Możesz użyć zadania VerifyJar w Ant, aby to zrobić. Oto link do pomocy Ant https://ant.apache.org/manual/Tasks/verifyjar.html

Przykładowy kod do weryfikacji wielu plików JAR naraz.

verifyjar keystore="mykeystore" keypass="abc" 
      storepass="abc" alias="myalias"> 
    <path> 
     <fileset dir="${build.dir}/signedjar" includes="**/*.jar" /> 
    </path> 
</verifyjar> 
Powiązane problemy