MavenプロジェクトにおけるJavaアノテーションの作成とjarパッケージ化による再利用例

最初のプロジェクト:

Linux環境の場合:

mkdir -p src/main/java/com/example/annotation

Windows環境の場合:

mkdir src\\main\\java\\com\\example\\annotation

どちらか一方を選択してください。 パス:G:\1\tryproductjava2\src\main\java\com\example\annotation

package com.example.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyCustomAnnotation {
   String value() default "";

   String name() default "";
}
package com.example.annotation;

@MyCustomAnnotation(
   value = "test",
   name = "MyService"
)
public class MyService {
   public MyService() {
   }

   public String sayHello() {
      return "Hello from MyService!";
   }

   public String sayHello(String name) {
      return "Hello, " + name + "!";
   }
}

プロジェクトルートディレクトリ(G:\1\tryproductjava2\)のpom.xmlファイル:

<?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.example</groupId>
    <artifactId>custom-annotation</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!-- アノテーション機能を提供するSpringコンテキスト -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.23</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

mvn clean packageコマンドを実行します。

二番目のプロジェクトでの利用手順:

mkdir -p spring-test\\src\\main\\java\\com\\example\\test
cd spring-test

G:\1\tryproductjava\spring-test\src\main\java\com\example\test

package com.example.test;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = {"com.example.annotation"})
public class AppConfig {
}
package com.example.test;

import com.example.annotation.MyCustomAnnotation;
import com.example.annotation.MyService;

public class SimpleTest {
    public static void main(String[] args) {
        // MyServiceのインスタンスを作成
        MyService myService = new MyService();
        
        // メソッド呼び出しのテスト
        System.out.println(myService.sayHello());
        System.out.println(myService.sayHello("Test"));
        
        // アノテーションの存在確認
        if (myService.getClass().isAnnotationPresent(MyCustomAnnotation.class)) {
            MyCustomAnnotation annotation = myService.getClass().getAnnotation(MyCustomAnnotation.class);
            System.out.println("アノテーションが見つかりました!");
            System.out.println("値: " + annotation.value());
            System.out.println("名前: " + annotation.name());
        } else {
            System.out.println("アノテーションは見つかりません!");
        }
        
        System.out.println("テストは正常に完了しました!");
    }
}

mvn clean compileコマンドを実行します。

javac -cp "../target/custom-annotation-1.0-SNAPSHOT.jar" src/main/java/com/example/test/SimpleTest.java -d target/classes
java -cp "target/classes;../target/custom-annotation-1.0-SNAPSHOT.jar" com.example.test.SimpleTest

タグ: Java Maven アノテーション jarパッケージ Spring Framework

5月20日 06:10 投稿