SPRING BOOT-如何从Super POM访问类

时间:2019-07-15 11:31:57

标签: java spring spring-boot inheritance pom.xml

我已经创建了一个父项目,因此我可以拥有所有常见项目中都将使用的所有常见依赖项,DTO,服务,助手等。

该父项目作为POM导入:

 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.whitehawk</groupId>
<artifactId>hawk-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hawk-parent</name>
<packaging>pom</packaging>
<description>Parent configuration for microservices</description>

在其他项目中,我有:

 <parent>
    <groupId>com.whitehawk</groupId>
    <artifactId>hawk-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<artifactId>customers</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>customers</name>
<description>Customers microservice</description>

依赖关系已正确导入,但是即使具有公共访问修饰符,我也无法访问例如我的Role枚举。

public enum Role {
    CUSTOMER,
    ADMIN,
    SUPERADMIN
}

或我的@UserRole批注:

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface UserRole {
    Role[] value();
}

关于我在做什么错的任何想法吗?

干杯!

更新

根据建议,我添加了一个新模块:hawk-core

它的POM看起来像:

    <?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>
    <parent>
        <groupId>com.whitehawk</groupId>
        <artifactId>hawk-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/>
    </parent>
    <artifactId>hawk-core</artifactId>
    <name>hawk-core</name>
    <description>Core libraries for microservices</description>
    <packaging>jar</packaging>
</project>

并将其包含在父项中:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.whitehawk</groupId>
<artifactId>hawk-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hawk-parent</name>
<packaging>pom</packaging>
<description>Parent configuration for microservices</description>

<modules>
    <module>hawk-core</module>
</modules>

1 个答案:

答案 0 :(得分:0)

我认为这有两点误解:

1-包装:如果您在jar / war文件中没有要打包的类,则此字段用作POM,例如,用于不用作依赖项或打包部署的父项目。

2-依存关系:这种礼节用于从您在此处拥有的示例中添加项目所需的lib包,因此您不包括该类,因此这些类将不会出现在您的项目中

建议有一个没有任何类且有2个模块的父项目,一个是您的核心类,以lib的形式作为jar,第二个是spring boot,您可以参考https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-build-systems.html#using-boot-maven-without-a-parent并将第一个模块作为第二个模块中的依赖关系如下:

parent
 |
 |-core
    |-pomt.xml
 |-app
    |-pomt.xml
 |-pom.xml
相关问题