没有模块化的Java 11:作为maven依赖项添加时,该包不存在 [英] Java 11 without modularity: package does not exist while it is added as maven dependency

查看:137
本文介绍了没有模块化的Java 11:作为maven依赖项添加时,该包不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个非常简单的应用程序.一个是COMMON,我将集中安全配置,另一个是ZUUL功能,第三个是EUREKA SERVER.两者都是Spring Boot和Java11.尽管我使用的是Java 11,但我并不依赖于模块化(据我所知,使用Spring Boot的模块化功能是没有道理的.)

当我在Eclipse中运行所有这三个服务器时,我可以按顺序成功运行服务器,common和zuul.当我尝试进行Maven构建时,出现主题中提到的错误.

直接的解释是您在zuul类路径中没有共同点".好吧,据我检查,我确实在zuul classpath中很常见.我想知道是否还有任何额外的配置,因为我使用的是Java 11,而不是模块化的.

说我正在使用Java 11,但不是模块化,我的意思是,我有Java 11,但是在这三个项目中的任何一个中我都没有module-info.java.

检入.m2文件夹并解压缩common.jar,我确实看到该类抱怨了.

Zuul项目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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.test</groupId>
    <artifactId>zuul</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>zuul</name>
    <description>Zuul project</description>

    <properties>
        <java.version>11</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</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-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt -->
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.9.1</version>
        </dependency>

        <dependency>
            <groupId>com.test</groupId>
            <artifactId>common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

常见的证据是行家依赖

MVN全新安装期间发生错误:

[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------------< com.test:zuul >----------------------------
[INFO] Building zuul 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ zuul ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ zuul ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to C:\_d\WSs\soteste\zuul\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /C:/_d/WSs/soteste/zuul/src/main/java/com/test/zuul/security/SecurityTokenConfig.java:[14,32] package com.test.common.security does not exist
[ERROR] /C:/_d/WSs/soteste/zuul/src/main/java/com/test/zuul/security/SecurityTokenConfig.java:[21,17] cannot find symbol
  symbol:   class JwtConfig
  location: class com.test.zuul.security.SecurityTokenConfig
[ERROR] /C:/_d/WSs/soteste/zuul/src/main/java/com/test/zuul/security/SecurityTokenConfig.java:[46,16] cannot find symbol
  symbol:   class JwtConfig
  location: class com.test.zuul.security.SecurityTokenConfig
[ERROR] /C:/_d/WSs/soteste/zuul/src/main/java/com/test/zuul/security/SecurityTokenConfig.java:[47,23] cannot find symbol
  symbol:   class JwtConfig
  location: class com.test.zuul.security.SecurityTokenConfig
[INFO] 4 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.778 s
[INFO] Finished at: 2019-03-21T14:23:23-03:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project zuul: Compilation failure: Compilation failure: 
[ERROR] /C:/_d/WSs/soteste/zuul/src/main/java/com/test/zuul/security/SecurityTokenConfig.java:[14,32] package com.test.common.security does not exist
[ERROR] /C:/_d/WSs/soteste/zuul/src/main/java/com/test/zuul/security/SecurityTokenConfig.java:[21,17] cannot find symbol
[ERROR]   symbol:   class JwtConfig
[ERROR]   location: class com.test.zuul.security.SecurityTokenConfig
[ERROR] /C:/_d/WSs/soteste/zuul/src/main/java/com/test/zuul/security/SecurityTokenConfig.java:[46,16] cannot find symbol
[ERROR]   symbol:   class JwtConfig
[ERROR]   location: class com.test.zuul.security.SecurityTokenConfig
[ERROR] /C:/_d/WSs/soteste/zuul/src/main/java/com/test/zuul/security/SecurityTokenConfig.java:[47,23] cannot find symbol
[ERROR]   symbol:   class JwtConfig
[ERROR]   location: class com.test.zuul.security.SecurityTokenConfig
[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

抱怨的教室在罐子里

***已编辑

普通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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.test</groupId>
    <artifactId>common</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>common</name>
    <description>Zuul project</description>

    <properties>
        <java.version>11</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

解决方案

这是因为 common 项目是一个Spring Boot Application(我在生成的jar中看到了BOOT-INF文件夹).

来自将Spring Boot应用程序用作依赖性文档:

可执行归档文件不能用作依赖项,因为 可执行jar格式将应用程序类打包在BOOT-INF/classes中. 这意味着在使用可执行jar时找不到它们 作为依赖项.

要产生两个工件,一个可以用作依赖项, 一个可执行文件,必须指定一个分类器.这 分类器应用于可执行存档的名称,保留 用作依赖项的默认存档.

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <classifier>exec</classifier>
            </configuration>
        </plugin>
    </plugins>
</build>

这里是一个例子:

第一个jar是可依赖构建的,第二个是Spring Boot可执行文件.

I have three very simple applilcations. One is COMMON which I will center security configs, other with ZUUL feature and the third is a EUREKA SERVER. Both are Spring Boot and Java 11. Although I am using Java 11 I am not reling on modularity (as far as I know it will not make sense use modularity feature with Spring Boot).

When I run all three in Eclipse I successfully can run the server, common and zuul in that sequence. When I try to maven build I get the error mentioned in subject.

The immediate explanation would be "you don't have common in zuul classpath". Well, as far as I check I do have common in zuul classpath. I am wondering if there is any extra config because I am using Java 11 but not using modularaty.

By saying I am using Java 11 but not modularity I mean, I have Java 11 but I don't have module-info.java at all in any of the three projects.

Checking in .m2 folder and unzip the common.jar I do see the class complained.

Zuul project 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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.test</groupId>
    <artifactId>zuul</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>zuul</name>
    <description>Zuul project</description>

    <properties>
        <java.version>11</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</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-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt -->
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.9.1</version>
        </dependency>

        <dependency>
            <groupId>com.test</groupId>
            <artifactId>common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Evidence that common is the maven dependences

Error during mvn clean install:

[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------------< com.test:zuul >----------------------------
[INFO] Building zuul 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ zuul ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ zuul ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to C:\_d\WSs\soteste\zuul\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /C:/_d/WSs/soteste/zuul/src/main/java/com/test/zuul/security/SecurityTokenConfig.java:[14,32] package com.test.common.security does not exist
[ERROR] /C:/_d/WSs/soteste/zuul/src/main/java/com/test/zuul/security/SecurityTokenConfig.java:[21,17] cannot find symbol
  symbol:   class JwtConfig
  location: class com.test.zuul.security.SecurityTokenConfig
[ERROR] /C:/_d/WSs/soteste/zuul/src/main/java/com/test/zuul/security/SecurityTokenConfig.java:[46,16] cannot find symbol
  symbol:   class JwtConfig
  location: class com.test.zuul.security.SecurityTokenConfig
[ERROR] /C:/_d/WSs/soteste/zuul/src/main/java/com/test/zuul/security/SecurityTokenConfig.java:[47,23] cannot find symbol
  symbol:   class JwtConfig
  location: class com.test.zuul.security.SecurityTokenConfig
[INFO] 4 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.778 s
[INFO] Finished at: 2019-03-21T14:23:23-03:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project zuul: Compilation failure: Compilation failure: 
[ERROR] /C:/_d/WSs/soteste/zuul/src/main/java/com/test/zuul/security/SecurityTokenConfig.java:[14,32] package com.test.common.security does not exist
[ERROR] /C:/_d/WSs/soteste/zuul/src/main/java/com/test/zuul/security/SecurityTokenConfig.java:[21,17] cannot find symbol
[ERROR]   symbol:   class JwtConfig
[ERROR]   location: class com.test.zuul.security.SecurityTokenConfig
[ERROR] /C:/_d/WSs/soteste/zuul/src/main/java/com/test/zuul/security/SecurityTokenConfig.java:[46,16] cannot find symbol
[ERROR]   symbol:   class JwtConfig
[ERROR]   location: class com.test.zuul.security.SecurityTokenConfig
[ERROR] /C:/_d/WSs/soteste/zuul/src/main/java/com/test/zuul/security/SecurityTokenConfig.java:[47,23] cannot find symbol
[ERROR]   symbol:   class JwtConfig
[ERROR]   location: class com.test.zuul.security.SecurityTokenConfig
[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

The class complained is in the jar

*** Edited

Common 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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.test</groupId>
    <artifactId>common</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>common</name>
    <description>Zuul project</description>

    <properties>
        <java.version>11</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

解决方案

That's because the common project is a Spring Boot Application (I see the BOOT-INF folder inside the generated jar).

From Use a Spring Boot Application as a Dependency documentation:

The executable archive cannot be used as a dependency as the executable jar format packages application classes in BOOT-INF/classes. This means that they cannot be found when the executable jar is used as a dependency.

To produce the two artifacts, one that can be used as a dependency and one that is executable, a classifier must be specified. This classifier is applied to the name of the executable archive, leaving the default archive for use as a dependency.

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <classifier>exec</classifier>
            </configuration>
        </plugin>
    </plugins>
</build>

Here is an example:

The first jar is build as dependency-able, the second one is Spring Boot executable.

这篇关于没有模块化的Java 11:作为maven依赖项添加时,该包不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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