Java コード ファイルには、@author タグと @version タグが含まれています。version タグには、ファイルのリビジョン番号に関する情報が含まれています。この情報はコメントにあります。この情報を .class ファイルに追加できるコンパイル フラグまたはその他のメカニズムはありますか?
2 に答える
簡単な答え: コンパイラは、JavaDoc を他のすべての形式のコメントとして無視します。
@author AuthorName
長い答え: 次のように、クラス/メソッド宣言の上に既存の@version VersionString
javadoc 要素をコピーするアプリケーションを作成する必要があります。
@Author({"AuthorName", "OtherAuthor"})
@Version("VersionString")
public class Something [...] { [...] }
Author
注釈の例は次のとおりです。
@Author("afk5min")
@Target({ ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.CLASS)
public @interface Author {
String[] value();
}
このようにして、各注釈が結果のクラス ファイルに存在し、アクセスできるようになります。
9.6.3.2. @保持
注釈は、ソース コードにのみ存在する場合もあれば、クラスまたはインターフェイスのバイナリ形式で存在する場合もあります。バイナリ形式で存在するアノテーションは、Java SE プラットフォームのリフレクション ライブラリを介して実行時に利用できる場合と利用できない場合があります。注釈タイプ java.lang.annotation.Retention は、これらの可能性の中から選択するために使用されます。
さらに、RetentionPolicy.RUNTIME
注釈への実行時アクセスが必要な場合に指定できます。リフレクション API では、次のことが可能です。
Author.class.getAnnotation(Author.class).value()[0] -> "afk5min"
ある時、Ant ビルド スクリプトを変更して、バージョン番号などの情報を含む MANIFEST.MF ファイルを作成しました。
これが最新の MANIFEST.MF ファイルです。
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.5
Created-By: 19.0-b09 (Sun Microsystems Inc.)
Main-Class: gov.bop.svnreport.SVNReportMain
Built-By: Gilbert G. Le Blanc
Built-On: 20111109-1437
Version: 1.1.1
Specification-Title: Subversion Commit Report
Specification-Version: 1.1.1
Specification-Vendor: Federal Bureau of Prisons
Class-Path: svnkit.jar
Ant スクリプトの JAR ターゲットは次のとおりです。
<target name="jar" depends="compile">
<mkdir dir = "${jar}"/>
<echo>Jar directory - "${jar}"</echo>
<manifest file="META-INF/MANIFEST.MF">
<attribute name="Main-Class" value="gov.bop.svnreport.SVNReportMain"/>
<attribute name="Class-Path" value="svnkit.jar"/>
<!-- This line puts your username into the manifest.
Maybe you don't want to do that. -->
<attribute name="Built-By" value="Gilbert G. Le Blanc"/>
<attribute name="Built-On" value="${DSTAMP}-${TSTAMP}"/>
<!-- This property was set by the svn-info task -->
<!-- <attribute name="Revision" value="${svnrevision}"/> -->
<!-- This property comes from the build.properties file -->
<attribute name="Version" value="${app.version}"/>
<attribute name="Specification-Title" value="Subversion Commit Report"/>
<attribute name="Specification-Version" value="${app.version}"/>
<attribute name="Specification-Vendor" value="Federal Bureau of Prisons"/>
</manifest>
<jar jarfile="${jar}/${ant.project.name}.jar"
manifest="META-INF/MANIFEST.MF">
<fileset dir="${build}/">
<patternset refid="all-classes"/>
</fileset>
<fileset dir="." includes="${bin.includes}/"/>
</jar>
<copy file="${jar}/${ant.project.name}.jar" todir="${deploy}" />
<copy file="${svnkit}" todir="${deploy}" />
</target>
Subversion のバージョン番号を MANIFEST.MF ファイルに入れようとしましたが、うまくいきませんでした。
この方法の問題は、JAR ファイルから Java アプリケーションを実行する場合にのみ機能することです。これらのマニフェスト プロパティを Eclipse などの IDE から取得しようとすると、null
値が返されます。