使用Maven Spring和Kotlin创建多模块项目获得未解决的参考 [英] Creating a multi module project with maven spring and kotlin get unresolved reference

查看:104
本文介绍了使用Maven Spring和Kotlin创建多模块项目获得未解决的参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Kotlin和Spring构建一个多模块Maven项目.当kotlin-maven-plugin运行编译阶段时,我得到一个错误.

I am trying to build a multi module maven project with kotlin and spring. I get an error when the kotlin-maven-plugin runs the compile phase.

[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ starter ---
[INFO] Deleting /Users/pnts/IdeaProjects/getyour/base/starter/target
[INFO] 
[INFO] --- git-commit-id-plugin:2.2.6:revision (default) @ starter ---
[INFO] 
[INFO] --- git-commit-id-plugin:2.2.6:revision (get-the-git-infos) @ starter ---
[INFO] 
[INFO] --- kotlin-maven-plugin:1.3.41:compile (compile) @ starter ---
[INFO] Applied plugin: 'spring'
[ERROR] /Users/pnts/IdeaProjects/getyour/base/starter/src/main/kotlin/org/getyour/webdesign/PlainStarter.kt: (6, 9) Unresolved reference: WebsiteEntryPoint
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for parent 0.0.1-SNAPSHOT:
[INFO] 
[INFO] parent ............................................. SUCCESS [  1.178 s]
[INFO] common ............................................. SUCCESS [  0.021 s]
[INFO] platform-api ....................................... SUCCESS [  4.238 s]
[INFO] platform-core ...................................... SUCCESS [  0.215 s]
[INFO] platform-bom ....................................... SUCCESS [  0.012 s]
[INFO] platform-parent .................................... SUCCESS [  0.061 s]
[INFO] starter ............................................ FAILURE [  0.343 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  6.399 s
[INFO] Finished at: 2019-09-27T08:57:04+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.jetbrains.kotlin:kotlin-maven-plugin:1.3.41:compile (compile) on project starter: Compilation failure
[ERROR] /Users/pnts/IdeaProjects/getyour/base/starter/src/main/kotlin/org/getyour/webdesign/PlainStarter.kt:[6,9] Unresolved reference: WebsiteEntryPoint
[ERROR] 
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
[ERROR] 
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <goals> -rf :starter

有趣的是,当我使用"mvn clean"清理构建并运行其编译的项目并运行spring应用程序时.

The funny thing is when I clean my build with "mvn clean" and run the project it compiles and run the spring application.

春天开始正常

我不知道是什么原因导致了这个问题.也许我想念一些东西.

I dont know what causes this issue. Maybe I am missing something.

这是我的poms和我的项目结构.希望有人可以帮助我.

Here are my poms and my project structure. Hope somebody can help me with this.

父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 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.1.8.RELEASE</version>
        <relativePath/>
    </parent>
    <version>0.0.1-SNAPSHOT</version>

    <artifactId>parent</artifactId>
    <groupId>org.getyour.webdesign</groupId>
    <packaging>pom</packaging>

    <modules>
        <module>common</module>
        <module>starter</module>
    </modules>

    <!-- properties -->
    <properties>
        <kotlin.version>1.3.41</kotlin.version>
        <spring.boot.version>2.1.8.RELEASE</spring.boot.version>
        <java.version>1.8</java.version>
        <main.class>org.getyour.webdesign.WebsiteEntryPoint</main.class>
    </properties>

    <!-- repositories -->
    <repositories>
        <repository>
            <id>maven-central</id>
            <url>http://central.maven.org/maven2/</url>
        </repository>
    </repositories>

    <build>
        <sourceDirectory>src/main/kotlin</sourceDirectory>
        <testSourceDirectory>src/test/kotlin</testSourceDirectory>
        <pluginManagement>
            <plugins>
                <plugin>
                    <!-- Import dependency management from Spring Boot -->
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-dependencies</artifactId>
                    <version>${spring.boot.version}</version>
                </plugin>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>${spring.boot.version}</version>
                </plugin>
                <plugin>
                    <groupId>org.jetbrains.kotlin</groupId>
                    <artifactId>kotlin-maven-plugin</artifactId>
                    <version>${kotlin.version}</version>
                </plugin>
            </plugins>
        </pluginManagement>

        <plugins>

            <!-- kotlin spring compiler plugin -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring.boot.version}</version>
                <configuration>
                    <mainClass>${main.class}</mainClass>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <version>${kotlin.version}</version>
                <executions>
                    <execution>
                        <id>compile</id>
                        <goals><goal>compile</goal></goals>
                        <phase>process-sources</phase>
                    </execution>
                    <execution>
                        <id>test-compile</id>
                        <goals><goal>test-compile</goal></goals>
                        <phase>process-test-sources</phase>
                    </execution>
                </executions>
                <configuration>
                    <args>
                        <arg>-Xjsr305=strict</arg>
                    </args>
                    <compilerPlugins>
                        <plugin>spring</plugin>
                    </compilerPlugins>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.jetbrains.kotlin</groupId>
                        <artifactId>kotlin-maven-allopen</artifactId>
                        <version>${kotlin.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.jetbrains.kotlin</groupId>
                        <artifactId>kotlin-stdlib-jdk8</artifactId>
                        <version>${kotlin.version}</version>
                    </dependency>
                </dependencies>
            </plugin>

            <plugin>
                <groupId>pl.project13.maven</groupId>
                <artifactId>git-commit-id-plugin</artifactId>
                <version>2.2.6</version>
                <executions>
                    <execution>
                        <id>get-the-git-infos</id>
                        <goals>
                            <goal>revision</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
                    <prefix />
                    <verbose>false</verbose>
                    <generateGitPropertiesFile>true</generateGitPropertiesFile>
                    <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties
                    </generateGitPropertiesFilename>
                    <format>properties</format>
                    <gitDescribe>
                        <skip>false</skip>
                        <always>false</always>
                        <dirty>-dirty</dirty>
                    </gitDescribe>
                </configuration>
            </plugin>

        </plugins>
    </build>

</project>

启动器pom.xml

starter 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>
    <parent>
        <artifactId>parent</artifactId>
        <groupId>org.getyour.webdesign</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <version>0.0.1-SNAPSHOT</version>

    <artifactId>starter</artifactId>
    <packaging>jar</packaging>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.getyour.webdesign</groupId>
                <artifactId>platform-bom</artifactId>
                <version>0.0.1-SNAPSHOT</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.getyour.webdesign</groupId>
            <artifactId>platform-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-reflect</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib-jdk8</artifactId>
        </dependency>
    </dependencies>

</project>

WebsiteEntryPoint.kt

WebsiteEntryPoint.kt

package org.getyour.webdesign

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

/**
 * Start class of the application
 *
 * @author p.tsivelekidis
 * Created by p.tsivelekidis on 2019-09-20
 */
@SpringBootApplication
class WebsiteEntryPoint {
    companion object {
        fun main(args: Array<String>) {
            runApplication<WebsiteEntryPoint>(*args)
            println("Hello!")
        }
    }
}

PlainStarter.kt

PlainStarter.kt

package org.getyour.webdesign

class PlainStarter

    fun main(args: Array<String>) {
        WebsiteEntryPoint.main(args)
    }

这是我的项目结构.注意,当我运行"mvn clean install"命令时,我得到了.

Here is my project structure. Notice that when I run the "mvn clean install" command I get this.

mvn全新安装后的项目结构

当我清理项目并运行时,我得到了这个结构.

When I clean my project and just run I get this structure.

清理并仅运行项目后的项目结构

当我运行mvn clean install时,它不会创建目标类,因此我猜测它找不到我的WebsiteEntryPoint.

When I run the mvn clean install it does not create the target classes and I guess for that reason it cant find my WebsiteEntryPoint.

希望有人可以帮助我.非常感谢.

Hope somebody can help me with that. Thanks a lot.

推荐答案

在用intellij编译kotlin多模块项目时,我遇到了同样的问题.命令 mvn clean install 产生了与未解决的依赖项相同的错误.

i had the same issue with compiling a kotlin multi module project in intellij. Command mvn clean install produced the same error with the unresolved dependencies.

正如我所看到的,您的源目录被命名为kotlin(与我之前的项目一样).

As i can see your source directories are named kotlin (as in my project before).

将我的项目中的源目录重命名为java后,问题已解决...

After renaming the source directories in my project to java the problem was solved...

这篇关于使用Maven Spring和Kotlin创建多模块项目获得未解决的参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆