Contents

Maven compile cannot find existing packages

When I setup java project using thrift, maven build fails because that cannot find thrift generated java sources. This is a memo for how I fix the configuration for goal of compile in maven.

Environment

  • Mac OS Monterey 12.0
  • Java 17.0.1 2021-10-19 LTS
  • Apache Maven 3.8.3

Problem

When I build java project by maven, mvn install raised error.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
$ mvn clean install package -e
...
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /Users/tato/repo/github/tatoflam/thrift-example/multiplication-app/src/main/java/io/hommalab/app/MultiplicationHandler.java:[5,68] package MultiplicationService does not exist
[ERROR] /Users/tato/repo/github/tatoflam/thrift-example/multiplication-app/src/main/java/io/hommalab/app/MultiplicationServer.java:[16,38] package MultiplicationService does not exist
...
[INFO] 10 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  6.268 s
[INFO] Finished at: 2021-11-14T23:15:32+09:00
[INFO] ------------------------------------------------------------------------
...
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project multiplication-app: Compilation failure
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:972)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:293)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:196)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:77)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:568)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
Caused by: org.apache.maven.plugin.compiler.CompilationFailureException: Compilation failure
    at org.apache.maven.plugin.compiler.AbstractCompilerMojo.execute (AbstractCompilerMojo.java:1215)
    at org.apache.maven.plugin.compiler.CompilerMojo.execute (CompilerMojo.java:188)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:972)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:293)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:196)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:77)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:568)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
[ERROR] 
[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

Project structure

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
├── multiplication-app
│   ├── pom.xml
│   ├── src
│   │   ├── main
│   │   │   ├── gen-java # This is generated directory by thrift command
│   │   │   │   └── io
│   │   │   │       └── hommalab
│   │   │   │           └── app
│   │   │   │               └── MultiplicationService.java
│   │   │   └── java
│   │   │       └── io
│   │   │           └── hommalab
│   │   │               └── app
│   │   │                   ├── MultiplicationClient.java
│   │   │                   ├── MultiplicationHandler.java
│   │   │                   └── MultiplicationServer.java
│   │   └── test
│   │       └── java
│   │           └── io
│   │               └── hommalab
│   │                   └── app
│   │                       └── xxxTest.java
│   └── target
└── multiplication.thrift

I made separated directory named src/main/gen-java for thrift generated code (created by thrift --gen java multiplication.thrift) as I wanted to distinguish that from application source code in src/main/java.

I moved the generated directory (gen-java) from project base directory to multiplication-app/src/main as that directory sounds like a good place to locate source in practice.


Cause

The error raised because maven compilation task cannot find src/main/gen-java/io/hommalab/app/MultiplicationService.java.


Solution

Checking former report, there are several ways to fix this. In my case, belows were useful.

  1. Add sources configuration to maven-compiler-plugin
  2. Use build-helper-maven-plugin

I adopted 2. because 1. raises the other warning saying jar is empty.

1. Add sources configuration to maven-compiler-plugin

pom.xml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>          
    <configuration>
    <includes>
        <include>src/main/java/**/*.java</include>
        <include>src/main/gen-java/**/*.java</include>
    </includes>
    </configuration> 
</plugin>

mvn install works for compilation, but this raises the other build error for generating jar.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
$ mvn clean install package -e
...
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ multiplication-app ---
[WARNING] JAR will be empty - no content was marked for inclusion!
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.344 s
[INFO] Finished at: 2021-11-15T00:45:58+09:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-jar-plugin:3.0.2:jar (default-jar) on project multiplication-app: You have to use a classifier to attach supplemental artifacts to the project instead of replacing them. -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-jar-plugin:3.0.2:jar (default-jar) on project multiplication-app: You have to use a classifier to attach supplemental artifacts to the project instead of replacing them.
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
...

There seems to be an odd workaround (add a single useless class in src/main/java?), the mail thread is not solved as of Nov 15 2021.

Ref. Stackoverflow | Maven JAR Plugin 3.0.2 Error: You have to use a classifier to attach supplemental artifacts to the project instead of replacing them

Removing maven-jar-plugin from plugins in pom.xml, the error is gone. However, the warning is saying that jar is empty.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
$ mvn clean install package -e
...
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ multiplication-app ---
[WARNING] JAR will be empty - no content was marked for inclusion!
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  6.541 s
[INFO] Finished at: 2021-11-14T23:05:54+09:00
[INFO] ------------------------------------------------------------------------

2. Use build-helper-maven-plugin

pom.xml

Add build-helper-maven-plugin to build/plugins

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<build>
    <plugins>
        <plugin>
        <artifactId>maven-project-info-reports-plugin</artifactId>
        <version>3.0.0</version>
        </plugin>
        <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>3.2.0</version>
        <executions>
            <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>add-source</goal>
            </goals>
            <configuration>
                <sources>
                <source>src/main/gen-java</source>
                </sources>
            </configuration>
            </execution>
        </executions>
        </plugin>
    </plugins>
</build>

mvn install works by this configuration.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
$ mvn clean install package -e
[INFO] Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------< io.hommalab.app:multiplication-app >-----------------
[INFO] Building multiplication-app 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ multiplication-app ---
[INFO] Deleting /Users/tato/repo/github/tatoflam/thrift-example/multiplication-app/target
[INFO] 
[INFO] --- build-helper-maven-plugin:3.2.0:add-source (default) @ multiplication-app ---
[INFO] Source directory: /Users/tato/repo/github/tatoflam/thrift-example/multiplication-app/src/main/gen-java added.
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ multiplication-app ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/tato/repo/github/tatoflam/thrift-example/multiplication-app/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ multiplication-app ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 5 source files to /Users/tato/repo/github/tatoflam/thrift-example/multiplication-app/target/classes
[INFO] /Users/tato/repo/github/tatoflam/thrift-example/multiplication-app/src/main/java/io/hommalab/app/MultiplicationServer.java: /Users/tato/repo/github/tatoflam/thrift-example/multiplication-app/src/main/java/io/hommalab/app/MultiplicationServer.java uses unchecked or unsafe operations.
[INFO] /Users/tato/repo/github/tatoflam/thrift-example/multiplication-app/src/main/java/io/hommalab/app/MultiplicationServer.java: Recompile with -Xlint:unchecked for details.
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ multiplication-app ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/tato/repo/github/tatoflam/thrift-example/multiplication-app/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ multiplication-app ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/tato/repo/github/tatoflam/thrift-example/multiplication-app/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ multiplication-app ---
[INFO] Surefire report directory: /Users/tato/repo/github/tatoflam/thrift-example/multiplication-app/target/surefire-reports
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running io.hommalab.app.AppTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.013 s - in io.hommalab.app.AppTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] 
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ multiplication-app ---
[INFO] Building jar: /Users/tato/repo/github/tatoflam/thrift-example/multiplication-app/target/multiplication-app-1.0-SNAPSHOT.jar
[INFO] 
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ multiplication-app ---
[INFO] Installing /Users/tato/repo/github/tatoflam/thrift-example/multiplication-app/target/multiplication-app-1.0-SNAPSHOT.jar to /Users/tato/.m2/repository/io/hommalab/app/multiplication-app/1.0-SNAPSHOT/multiplication-app-1.0-SNAPSHOT.jar
[INFO] Installing /Users/tato/repo/github/tatoflam/thrift-example/multiplication-app/pom.xml to /Users/tato/.m2/repository/io/hommalab/app/multiplication-app/1.0-SNAPSHOT/multiplication-app-1.0-SNAPSHOT.pom
[INFO] 
[INFO] --- build-helper-maven-plugin:3.2.0:add-source (default) @ multiplication-app ---
[INFO] Source directory: /Users/tato/repo/github/tatoflam/thrift-example/multiplication-app/src/main/gen-java added.
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ multiplication-app ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/tato/repo/github/tatoflam/thrift-example/multiplication-app/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ multiplication-app ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ multiplication-app ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/tato/repo/github/tatoflam/thrift-example/multiplication-app/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ multiplication-app ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ multiplication-app ---
[INFO] Skipping execution of surefire because it has already been run for this configuration
[INFO] 
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ multiplication-app ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.570 s
[INFO] Finished at: 2021-11-15T01:01:43+09:00
[INFO] ------------------------------------------------------------------------
the location for adding `build-helper-maven-plugin`
In my case, I needed to add above at the plugins section, not in pluginManagement section.

Sources

github


References