枚举不存在 Maven 包 [英] Maven package does not exist for enum

查看:70
本文介绍了枚举不存在 Maven 包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标:
- 我想将一个 API 测试框架 X 代码(一个 maven 项目)转换成一个 jar在另一个API 测试框架中使用
- 这个 jar 将被部署到 Nexus
- 我想要测试&一个jar

My goal:
- I want to convert an API test framework X code (a maven project) into a jar which can be used inside another API test framework Y
- This jar will be deployed to Nexus
- I wanted the test & main classes of framework X in one jar

SO 中,我了解到您不能将它们组合成一个 jar,因此您必须将它们放在单独的 jar 中.

From SO, I learned that you cannot combine them into one jar, so you have to have them in separate Jars.

我相应地修改了 POM.

当我使用这个命令时出现编译错误:

I get a compilation error when I use this command:

mvn clean install -DskipTests

mvn clean install -DskipTests

错误片段:

[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /C:/main/projectX/src/main/java/com/myapp/core/EnumUserClass.java:[8,46] package com.myapp.testutils.ClassWithEnum does not exist
[ERROR] /C:/main/projectX/src/main/java/com/myapp/core/EnumUserClass.java:[226,6] cannot find symbol
  symbol:   class EnumUserClass
  location: class com.myapp.core.EnumUserClass...etc.

Maven 编译器在查找类时没有问题,但是在查找此 enum 时却出现了问题.

Maven compiler has no problem in finding classes, but it has a problem when finding this enum.

谁能告诉我如何正确解决此错误?我是否以正确的方式实现了我的目标"?

这个问题中给出的示例项目(projectX)是我实际项目的一个简短版本.
maven 命令适用于 example project,但不适用于 actual project.

The example project (projectX) given in this question is a short version of my actual project.
The maven command works fine for the example project, but not for the actual project.

示例项目结构:(简化)

projectX:
    src/test/Java
        com.myapp.testutils
            class ClassWithEnum - This contains a public enum MyEnum
    src/test/resources
        text files here
    src/main/Java
        com.myapp.core
            class EnumUserClass - This is jackson annotated & uses MyEnum

示例项目 pom.xml:(缩短)

<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>

    <groupId>com.myapp</groupId>
    <artifactId>my-jar</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <build>
        <defaultGoal>install</defaultGoal>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>test-jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <name>myjar</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!--Many dependencies here-->
    </dependencies>

    <distributionManagement>
        <site>
            <id>My-Nexus-Site</id>
            <url>https://host.com/path1</url>
        </site>
        <repository>
            <id>My-Nexus-Releases</id>
            <name>My-Nexus-Releases</name>
            <url>https://host.com/path2</url>
        </repository>
        <snapshotRepository>
            <id>My-Nexus-Snapshots</id>
            <name>My-Nexus-Snapshots</name>
            <url>https://host.com/path3</url>
        </snapshotRepository>
    </distributionManagement>
</project>

推荐答案

你的问题实际上有 3 个问题(1 你的目标和 2 个问题).但是,它们是组合在一起的,可以固定在一起.

Your question has actually 3 questions (1 your goal and 2 questions to it). However, they are combined and can be fixed together.

  1. 谁能告诉我如何正确解决这个错误?:

这里的问题是Maven有自己的生命周期.

The problem here is that Maven has its own lifecycle.

它具有严格的后果:

It has a strict consequence:

  • compile - 编译项目的源代码
  • test - 使用合适的单元测试框架测试编译的源代码.这些测试不应该要求打包或部署代码
  • compile - compile the source code of the project
  • test - test the compiled source code using a suitable unit testing framework. These tests should not require the code to be packaged or deployed

并且在 compile 期间,您只能编译您的源代码.
但是,这取决于您的测试类(枚举),此时无法编译,因为它只会在 test 阶段编译.
因此编译失败.

And during compile you can compile only your sources.
However, it depends on your tests classes (enum), it can't be compiled at this moment, because it will compile only at test phase.
Thus compilation fails.

为了解决尝试将枚举类移动到src/main/java.它应该可以解决这个编译错误.

For solving try to move enum class to src/main/java. It should solve this compilation error.

  1. 我是否以正确的方式实现了我的目标"?:

为了实现这个目标测试&一个 jar 中框架 X 的主要类 你还有另一种选择.你可以移动你所有的测试类 &资源(不仅是枚举类)到 src/main/javasrc/main/resources:

For achieving this goal test & main classes of framework X in one jar you have another alternative. You can move all your test classes & resources (not only enum class) to src/main/java and src/main/resources:

projectX:
    src/main/java
        com.myapp.core
            class EnumUserClass - This is jackson annotated & uses MyEnum
        com.myapp.testutils
            class ClassWithEnum - This contains a public enum MyEnum
    src/main/resources
        text files here

之后,您必须将所有 pom 范围更改为 compile 并更新 jar-plugin.

After it, you have to change all pom scopes to compile and update jar-plugin as well.

最后,用 test & 构建一个 jar 文件主类.你可以从 projectY

Finally, build one jar file with test & main classes. And you can use it from projectY

更多信息:如何创建一个包含测试类的 jar

这篇关于枚举不存在 Maven 包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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