多模块项目

时间:2019-06-26 14:12:28

标签: java spring maven spring-boot

这是子项目的pom.xml,aragorn-web是父项目:

<?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.caner</groupId>
        <artifactId>aragorn-web</artifactId>
        <version>1.0</version>
    </parent>

    <artifactId>aragorn-internal</artifactId>
    <version>1.0</version>
    <packaging>war</packaging>


    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
            <version>3.0.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
        </dependency>
        <!-- Spring Security -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
            <version>3.0.11.RELEASE</version>
        </dependency>
    </dependencies>


</project>

这是要运行的Java类

package com.caner;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

    @SpringBootApplication
    public class AragornInternalApplication{

        public static void main(String[] args) {
            SpringApplication.run(AragornInternalApplication.class, args);
        }

    }

当我运行该子模块时,它给出了错误:

Connected to the target VM, address: '127.0.0.1:58367', transport: 'socket'
Error: Could not find or load main class com.caner.AragornInternalApplication
Disconnected from the target VM, address: '127.0.0.1:58367', transport: 'socket'

Process finished with exit code 1

当我转到intellij的“问题”标签时,几乎所有类都遇到很多错误。但是对于AragornInternalApplication.java:

Error:(3, 32) java: package org.springframework.boot does not exist
Error:(4, 46) java: package org.springframework.boot.autoconfigure does not exist
Error:(6, 2) java: cannot find symbol
  symbol: class SpringBootApplication
Error:(10, 9) java: cannot find symbol
  symbol:   variable SpringApplication
  location: class com.caner.AragornInternalApplication

但是我有spring-boot

            <artifactId>spring-boot-starter-web</artifactId>

主要pom xml为:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <!--2.1.4.RELEASE-->
        <!--2.1.3.RELEASE-->
        <!--2.1.2.RELEASE-->
        <relativePath/>
    </parent>

    <groupId>com.caner</groupId>
    <artifactId>aragorn-web</artifactId>
    <version>1.0</version>
    <name>aragorn-web</name>
    <packaging>pom</packaging>

    <modules>
        <module>aragorn-backend</module>
        <module>aragorn-external</module>
        <module>aragorn-internal</module>
    </modules>

</project>

我从其他子pom复制了pom,但是它不起作用。

当我进入Intellij的模块部分时,我可以看到所有选项,例如路径,资源与那个孩子相同。

这是另一个孩子pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         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.caner</groupId>
        <artifactId>aragorn-web</artifactId>
        <version>1.0</version>
    </parent>

    <artifactId>aragorn-external</artifactId>
    <version>1.0</version>
    <name>aragorn-external</name>
    <!--    <description>caner E-book (Project:aragorn)</description>-->
    <packaging>war</packaging>

    <properties>
        <java.version>1.8</java.version>
        <hibernate-core.version>5.3.7.Final</hibernate-core.version>
        <hibernate-commons-annotations.version>5.0.4.Final</hibernate-commons-annotations.version>
        <commons-codec.version>1.9</commons-codec.version>
        <mysql-connector-java.version>5.1.46</mysql-connector-java.version>
        <joda-time.version>2.9.9</joda-time.version>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf -->
        <dependency>
            <groupId>com.caner</groupId>
            <artifactId>aragorn-backend</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>nz.net.ultraq.thymeleaf</groupId>
            <artifactId>thymeleaf-layout-dialect</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>central</id>
            <url>http://repo1.maven.org/maven2/</url>
        </repository>
        <repository>
            <id>hibernate-validator</id>
            <url>https://mvnrepository.com/artifact/org.hibernate.validator/hibernate-validator</url>
        </repository>
        <repository>
            <id>hibernate-core</id>
            <url>https://mvnrepository.com/artifact/org.hibernate/hibernate-core</url>
        </repository>
        <repository>
            <id>hibernate-commons-annotations</id>
            <url>https://mvnrepository.com/artifact/org.hibernate.common/hibernate-commons-annotations</url>
        </repository>
        <repository>
            <id>reflections</id>
            <url>https://mvnrepository.com/artifact/org.reflections/reflections</url>
        </repository>
        <!--<repository>-->
        <!--<id>joda</id>-->
        <!--<url>https://mvnrepository.com/artifact/joda-time/joda-time</url>-->
        <!--</repository>-->
        <repository>
            <id>apache commons-codec</id>
            <url>https://mvnrepository.com/artifact/commons-codec/commons-codec</url>
        </repository>
    </repositories>

    <build>
        <finalName>ROOT</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12</version>
                <configuration>
                    <junitArtifactName>junit:junit</junitArtifactName>
                    <argLine>
                        <!--                        -Dspring.profiles.active=dev-->
                        -Xms256m -Xmx512m -ea -Dfile.encoding=UTF-8
                    </argLine>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

我重新启动,使缓存无效,删除了所有依赖关系,添加了其他依赖关系,但无法使其正常工作。

我也尝试转换为jar,但仍然相同

   <packaging>jar</packaging>

请帮助

1 个答案:

答案 0 :(得分:0)

如果您的父母不是spring-boot-starter-parent父pom,则必须将依赖项管理添加到父pom:

<dependencyManagement>
    <dependencies>
        <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.1.6.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

在官方文档中了解更多信息:https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-maven-without-a-parent