Project 1: pom.xml:
<dependency>
<groupId>project2</groupId>
<artifactId>project2</artifactId>
<version>{someversion}</version>
</dependency>
project 2: another maven spring project
project 1 and project 2 both are spring maven projects.From above architecture project 2 has been added as a library to project 1. My question is can I invoke project 1 class from project 2 using reflection?
In general yes, you can access of project2 from project1 at runtime. It will work both by "regular" class invocation and by reflection .
Otherwise why did you add the dependency? ;)
One caveat though: You state that both projects are "maven spring projects". You haven't explicitly stated the spring boot, but if project2 is a spring boot project and the jar is created with the help of spring-boot-maven-plugin
, its impossible to make such a dependency because project2.jar
is not really a jar (although it has the.jar extension).
Adding the Library Dependency
The Application project needs to have a dependency on the Library project. You need to modify your Application build file accordingly. More Details
For Maven, add the following dependency:
<dependency>
<groupId>com.example</groupId>
<artifactId>library</artifactId>
<version>${project.version}</version>
</dependency>COPY
The following listing shows the finished pom.xml file:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>multi-module-application</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>multi-module-application</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>library</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
We can invoke project 1 classes from project 2 classes using reflection.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.