1

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

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

I modified the POM accordingly.

I get a compilation error when I use this command:

mvn clean install -DskipTests

Error snippet:

[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 compiler has no problem in finding classes, but it has a problem when finding this enum.

Can someone please tell me how I can resolve this error properly? Am I achieving "my goal" in the correct way?

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.

Example Project Structure: (simplified)

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

Example Project pom.xml: (shortened)

<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>
catch23
  • 17,519
  • 42
  • 144
  • 217
MasterJoe
  • 2,103
  • 5
  • 32
  • 58
  • It seems like you are trying to import the test-class into your core (main classes). That wont work – Mr.Turtle Sep 26 '18 at 05:50
  • @OleReidarHolm - How can I fix this problem ? Should I create one copy of enum for test & one for main (not nice !) ? Is it possible to convert this project into a single jar, rather than 2 jars i.e main & test jar ? – MasterJoe Sep 26 '18 at 05:53
  • @OleReidarHolm - I have classes in Test which use code of classes in Main. This enum seems to be the problem. – MasterJoe Sep 26 '18 at 05:54
  • Hmm. What are you trying to do exactly? Why would you want one jar with the tests and main? Sorry for not understanding correctly, but the test classes are there to check whether or not the main are doing its job. You should not have "test"-code in your "main" folder. Have you seen this? https://stackoverflow.com/questions/8751553/how-to-write-a-unit-test But to answer one of your questions: I don't think it is possible (or necessary) to combine test and main in one jar. – Mr.Turtle Sep 26 '18 at 06:00
  • @OleReidarHolm - I have mentioned what I am trying to under "My goal". The projectX has utility code which is needed by projectY. Some of that code is in proectX main and the rest in in projectX test. The code in projectX is NOT an application ! Its just some QA/testing code. Let me know if I can clarify further. – MasterJoe Sep 26 '18 at 06:03
  • @OleReidarHolm - Remember, the "example project" 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". – MasterJoe Sep 26 '18 at 06:09

3 Answers3

3

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

  1. Can someone please tell me how I can resolve this error properly?:

The problem here is that Maven has its own lifecycle.

It has a strict consequence:

  • 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

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.

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

  1. Am I achieving "my goal" in the correct way?:

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

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

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

More information: How to create a jar containing test classes

catch23
  • 17,519
  • 42
  • 144
  • 217
2

you can access classes from "main/java" in "test/java", it does not work in reverse. if you check the log it is it is looking for a package in main/java. Move the package from "test/java" to "main/java". It should work fine then.

0

Ok, I understand what you are trying to do, but I believe that this is not the correct way of doing so. You want to use this project (X) to test another project (Y), right? Then, all the main classes in this project (X) are for testing project (Y).

But here is the issue: it seems like you are trying to put test classes (in the folder com.myapp.testutils) in the main folder. This causes the error:

[ERROR] /C:/main/projectX/src/main/java/com/myapp/core/EnumUserClass.java:[8,46] package com.myapp.testutils.ClassWithEnum does not exist

The test classes in (com.myapp.testutils) are for testing the main code of project X. These classes exists to check if the main code of project X is doing what it is supposed to do (in this context --> to test another project).

Unit tests (the classes in com.myapp.testutils) are not supposed to be wrapped in a jar, but it is made to make sure that the application is working properly.

How to write a Unit Test?

Your focus shouldn't be getting the tests from project X into one single jar, but rather making unit tests for Project X, and then import the properly tested project X into Project Y.

Edit: If the com.myapp.testutils are important to make project X able to test project Y: move the package from "test/java" to "main/java". It should work fine then.

Mr.Turtle
  • 2,950
  • 6
  • 28
  • 46