달력

4

« 2024/4 »

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30


jar 파일이 변경-버전업, 또는 이름 변경-될 때마다 eclipse에서 library 정보도 변경해줘야 되고,
개발자들에게 배포된 것도 확인해줘야 되고, 등등등. 이래저래 구찮은 일이 많아서.
그냥 jar 파일명에서 버전을 뺀 상태에서 버전관리만 하려고 찾다보니.
Manifest.MF 파일에 있는 버전 정보를 읽으면 되겠다 싶어서 만든 샘플.


checkVersion.java

import java.util.*;

public class checkVersion {
        JarFile jarFile;
        Manifest manifest;
        Attributes attributes;
        String jarFileName;
        String version;

        public checkVersion(String filename) {
                jarFileName = filename;
        }

        public void printVersion() {
                try {
                        // jar 파일을 읽습니다.
                        jarFile = new JarFile(new File(jarFileName));
                        // jar 파일에서 manifest 정보를 가져옵니다.
                        manifest = jarFile.getManifest();
                        // manifest에서 main 정보를 가져옵니다.
                        attributes = (Attributes)manifest.getMainAttributes();

                        // attribute 중에서 Implementation-Version 정보를 추출한다.
                        version = attributes.getValue("Implementation-Version");
                        System.out.println("@@ " + version);

                        /* 아래처럼 하면 manifest의 main 정보 전체를 확인할 수 있다.
                        for (Iterator it=attributes.keySet().iterator(); it.hasNext(); ) {
                                Attributes.Name attrName = (Attributes.Name)it.next();
                                String attrValue = attributes.getValue(attrName);

                                System.out.println("## " + attrName + " : " + attrValue);
                        }
                        */
                } catch(Exception e) {
                        e.printStackTrace();
                }
        }

        public static void main(String[] args) {
                if( args.length != 1 ) {
                        System.out.println("Usage : ");
                        System.out.println("    java printVersion [jar_file_name]");
                        return;
                }

                /*
                if( !(new File(args[0])).exists() ) {
                        System.out.println("Error : ");
                        System.out.println("[" + args[0] + "] file does not exist.");
                        return;
                }
                */

                checkVersion cv = new checkVersion(args[0]);
                cv.printVersion();
        }
}



 MANIFEST.MF


Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: Niall Pemberton
Build-Jdk: 1.5.0_07
Bundle-License: http://www.apache.org/licenses/LICENSE-2.0.txt
Bundle-ManifestVersion: 2
Bundle-Name: Apache Commons IO Bundle
Bundle-SymbolicName: org.apache.commons.io
Bundle-Vendor: The Apache Software Foundation
Bundle-Version: 1.4
Export-Package: org.apache.commons.io;version=1.4,
 org.apache.commons.io.comparator;version=1.4,
 org.apache.commons.io.filefilter;version=1.4,
 org.apache.commons.io.input;version=1.4,
 org.apache.commons.io.output;version=1.4
Implementation-Title: Commons IO
Implementation-Vendor: The Apache Software Foundation
Implementation-Vendor-Id: org.apache
Implementation-Version: 1.4
Import-Package: org.apache.commons.io;version=1.4,
 org.apache.commons.io.comparator;version=1.4,
 org.apache.commons.io.filefilter;version=1.4,
 org.apache.commons.io.input;version=1.4,
 org.apache.commons.io.output;version=1.4
Specification-Title: Commons IO
Specification-Vendor: The Apache Software Foundation
Specification-Version: 1.4
X-Compile-Source-JDK: 1.3
X-Compile-Target-JDK: 1.3 


실행결과

weblogic@WAS01[/export/home/weblogic]# java checkVersion commons-net-1.4.1.jar
@@ 1.4.1


 

'그거 > Java' 카테고리의 다른 글

XML 특수 문자 파싱 오류 확인  (0) 2010.01.29
[Java] Inner Class에 이런 것이 있었군요...  (0) 2009.09.29
JMeter 사용하기...  (2) 2009.08.20
JCONSOLE 사용하기.  (0) 2009.07.28
[ShutdownHook] 오랜만의 Java 포스팅..  (0) 2009.07.18
:
Posted by 뽀기