如何将Java代码文件的修订号插入Java类文件中

时间:2013-04-25 12:18:09

标签: java version-control cvs

Java代码文件包含@author和@version标记。 version标签包含有关文件修订号的信息。此信息位于评论中。 是否有可用的编译标志或其他机制,我可以将此信息添加到.class文件中?

2 个答案:

答案 0 :(得分:0)

简短回答: 编译器将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。 @retention

     

注释可能仅存在于源代码中,或者它们可能以类或接口的二进制形式存在。二进制形式中存在的注释可能在运行时通过Java SE平台的反射库提供,也可能不提供。注释类型java.lang.annotation.Retention用于在这些可能性中进行选择。

此外,如果您希望对注释进行运行时访问,则可以指定RetentionPolicy.RUNTIME。反射API允许这样:

Author.class.getAnnotation(Author.class).value()[0] -> "afk5min"

答案 1 :(得分:0)

有一次,我修改了我的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(如Eclipse)中提取这些清单属性,则会返回null值。

相关问题