美文网首页
java第一次打包

java第一次打包

作者: heliping_peter | 来源:发表于2018-02-23 14:27 被阅读14次

    1. 源代码,从网上下载,将其中的公有类分解

    /***********注解声明***************/
    
    /**
     * 水果名称注解
     * @author peida
     *
     */
    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface FruitName {
        String value() default "";
    }
    
    /**
     * 水果颜色注解
     * @author peida
     *
     */
    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface FruitColor {
        /**
         * 颜色枚举
         * @author peida
         *
         */
        public enum Color{ BULE,RED,GREEN};
        
        /**
         * 颜色属性
         * @return
         */
        Color fruitColor() default Color.GREEN;
    
    }
    
    /**
     * 水果供应者注解
     * @author peida
     *
     */
    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface FruitProvider {
        /**
         * 供应商编号
         * @return
         */
        public int id() default -1;
        
        /**
         * 供应商名称
         * @return
         */
        public String name() default "";
        
        /**
         * 供应商地址
         * @return
         */
        public String address() default "";
    }
    
    /***********注解使用***************/
    
    public class Apple {
        
        @FruitName("Apple")
        private String appleName;
        
        @FruitColor(fruitColor=Color.RED)
        private String appleColor;
        
        @FruitProvider(id=1,name="陕西红富士集团",address="陕西省西安市延安路89号红富士大厦")
        private String appleProvider;
        
        public void setAppleColor(String appleColor) {
            this.appleColor = appleColor;
        }
        public String getAppleColor() {
            return appleColor;
        }
        
        public void setAppleName(String appleName) {
            this.appleName = appleName;
        }
        public String getAppleName() {
            return appleName;
        }
        
        public void setAppleProvider(String appleProvider) {
            this.appleProvider = appleProvider;
        }
        public String getAppleProvider() {
            return appleProvider;
        }
        
        public void displayName(){
            System.out.println("水果的名字是:苹果");
        }
    }
    
    /***********注解处理器***************/
    
    public class FruitInfoUtil {
        public static void getFruitInfo(Class<?> clazz){
            
            String strFruitName=" 水果名称:";
            String strFruitColor=" 水果颜色:";
            String strFruitProvicer="供应商信息:";
            
            Field[] fields = clazz.getDeclaredFields();
            
            for(Field field :fields){
                if(field.isAnnotationPresent(FruitName.class)){
                    FruitName fruitName = (FruitName) field.getAnnotation(FruitName.class);
                    strFruitName=strFruitName+fruitName.value();
                    System.out.println(strFruitName);
                }
                else if(field.isAnnotationPresent(FruitColor.class)){
                    FruitColor fruitColor= (FruitColor) field.getAnnotation(FruitColor.class);
                    strFruitColor=strFruitColor+fruitColor.fruitColor().toString();
                    System.out.println(strFruitColor);
                }
                else if(field.isAnnotationPresent(FruitProvider.class)){
                    FruitProvider fruitProvider= (FruitProvider) field.getAnnotation(FruitProvider.class);
                    strFruitProvicer=" 供应商编号:"+fruitProvider.id()+" 供应商名称:"+fruitProvider.name()+" 供应商地址:"+fruitProvider.address();
                    System.out.println(strFruitProvicer);
                }
            }
        }
    }
    
    /***********输出结果***************/
    public class FruitRun {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            
            FruitInfoUtil.getFruitInfo(Apple.class);
            
        }
    
    }
    
    ====================================
     水果名称:Apple
     水果颜色:RED
     供应商编号:1 供应商名称:陕西红富士集团 供应商地址:陕西省西安市延安路89号红富士大厦
    

    2. pom文件,由于多个公有类,需要指定mainclass

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.apple</groupId>
        <artifactId>apple</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>applerun</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

    3. 目录结构

    image.png

    4. 使用maven complie然后package

    生成jar包,然后使用java -jar执行


    image.png

    相关文章

      网友评论

          本文标题:java第一次打包

          本文链接:https://www.haomeiwen.com/subject/eglxxftx.html