2

this answer did not solve my problem...I'm sure there is no spelling mistakes or something else, the Idea can run the application successfully sometimes, but not all the times.It may have some mistakes in POM file and I didn't find out. According to the answer in floor 2 by dreamt, it passed the maven compilation, but what if I insist to put it as a child of web-common?

I'm not quite sure what's the key problem. The compilation problem is occured while compiling ref-model, the constants package was missing which was inherited from web-common-model which was depended on web-common-enums.

I'm developing an application with springboot.
the project structure is shown below:

TestSplitCommon
    |____web-common
    |         |___web-common-enums
    |         |            |
    |         |___web-common-model
    |                      |____ref-model
    |
    |____other-service(which has dependency in web-common)

but I have found something wrong to my maven while compiling.
While installing by command mvn clean install it shows cannot find symbol Constants. However, the missing symbol Constants was specified in web-common-enums, and also, the dependency web-common-enums is properly set in the pom ofweb-common-model and such code is reachable in IDE. enter image description here


I don't know how to set `ref-model` as a dependency of other-service...

below is part of my code, and you can also get it in my source code

parent 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">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.wwstation</groupId>
    <artifactId>TestSplitCommon</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>web-common</module>
        <module>other-service</module>
    </modules>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>30.1-jre</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
    </dependencies>
</project>

web-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">
    <parent>
        <artifactId>TestSplitCommon</artifactId>
        <groupId>com.wwstation</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>web-common</artifactId>
    <packaging>pom</packaging>
    <modules>
        <module>web-common-enums</module>
        <module>web-common-model</module>

    </modules>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <layout>
                        NONE
                    </layout>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

ref-model 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">
    <parent>
        <artifactId>web-common</artifactId>
        <groupId>com.wwstation</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>web-common-model</artifactId>
    <packaging>pom</packaging>
    <modules>
        <module>ref-model</module>
    </modules>
    <dependencies>
        <dependency>
            <groupId>com.wwstation</groupId>
            <artifactId>web-common-enums</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <layout>
                        NONE
                    </layout>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

web-common-enum 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">
    <parent>
        <artifactId>web-common</artifactId>
        <groupId>com.wwstation</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>web-common-enums</artifactId>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <layout>
                        NONE
                    </layout>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

other-service 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">
    <parent>
        <artifactId>TestSplitCommon</artifactId>
        <groupId>com.wwstation</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>other-service</artifactId>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.wwstation</groupId>
            <artifactId>ref-model</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <goals>
                    <goal>
                        repackage
                    </goal>
                </goals>
            </plugin>
        </plugins>
    </build>
</project>

Constant class in web-common-enums

package com.wwstation.common.constants;

import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * 常量值
 *
 * @author william
 * @description
 * @Date: 2021-02-17 11:56
 */
@Getter
@AllArgsConstructor
public class Constants {
    public static final String commonString="commonString";
}

a simple class in ref-model

package com.wwstation.common;

import com.wwstation.common.constants.Constants;

/**
 * @author william
 * @description
 * @Date: 2022-01-28 12:04
 */
public class TestClass {
    public static void test() {
        System.out.println(Constants.commonString);
    }
}

main class in other-service

package com.wwstation.other_service;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author william
 * @description
 * @Date: 2022-01-28 14:09
 */
@SpringBootApplication(scanBasePackages = {"com.wwstation","com.wwstation.common"})
public class OtherServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(OtherServiceApplication.class, args);
    }
}

test component in other-service

package com.wwstation.other_service.components;


import com.wwstation.common.TestClass;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @author william
 * @description
 * @Date: 2022-01-28 14:12
 */
@Component
public class TestComponent {

    @PostConstruct
    public void initAndPrint() {
        TestClass.test();
    }
}

And this is the error console:

NFO] Compiling 1 source file to D:\TestSplitCommon\web-common\web-common-model\ref-model\target\classes
[DEBUG] incrementalBuildHelper#afterRebuildExecution
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /D:/TestSplitCommon/web-common/web-common-model/ref-model/src/main/java/com/wwstation/common_ref_model/TestClass.java:[3,33] 程序包com.wwstation.commonenums不存在
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for TestSplitCommon 1.0-SNAPSHOT:
[INFO]
[INFO] TestSplitCommon .................................... SUCCESS [  0.462 s]
[INFO] web-common ......................................... SUCCESS [  1.136 s]
[INFO] web-common-enums ................................... SUCCESS [  6.179 s]
[INFO] web-common-model ................................... SUCCESS [  0.387 s]
[INFO] ref-model .......................................... FAILURE [  2.855 s]
[INFO] other-service ...................................... SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  11.571 s
[INFO] Finished at: 2022-01-28T16:27:38+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project ref-model: Compilation failure
[ERROR] /D:/TestSplitCommon/web-common/web-common-model/ref-model/src/main/java/com/wwstation/common_ref_model/TestClass.java:[3,33] 程序包com.wwstation.commonenums不存在
[ERROR]
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project ref-model: Compi
lation failure
/D:/TestSplitCommon/web-common/web-common-model/ref-model/src/main/java/com/wwstation/common_ref_model/TestClass.java:[3,33] 程序包com.wwstation.commonenums不存在

    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:957)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    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
/D:/TestSplitCommon/web-common/web-common-model/ref-model/src/main/java/com/wwstation/common_ref_model/TestClass.java:[3,33] 程序包com.wwstation.commonenums不存在

    at org.apache.maven.plugin.compiler.AbstractCompilerMojo.execute (AbstractCompilerMojo.java:1220)
    at org.apache.maven.plugin.compiler.CompilerMojo.execute (CompilerMojo.java:187)
    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:957)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    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]
[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 <args> -rf :ref-model

william
  • 265
  • 4
  • 17
  • Without a [mre] this is hard to diagnose. – Mark Rotteveel Jan 28 '22 at 07:33
  • @MarkRotteveel the source code was [here](https://github.com/WilliamChen-luckbob/SplitCommonDemo). you can get it and have a look, I have made a simple demo to print a simple word in terminal, it will lead to my problem. – william Jan 28 '22 at 07:38
  • A question should be self-contained and not rely on external links for its [mre]. – Mark Rotteveel Jan 28 '22 at 07:39
  • its hard to say where the problem is, so I pasted all my source code into this question. And the demo is the minimal reproducible example, the problem will not reproduced without any of the modules.... – william Jan 28 '22 at 08:21

2 Answers2

0

I removed the relationship between 'web-common' module and 'web-common-enums' module on pom.xml file:

<?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">
<groupId>com.wwstation</groupId>
<modelVersion>4.0.0</modelVersion>
<version>1.0-SNAPSHOT</version>
<artifactId>web-common-enums</artifactId>
</project>

located in '${root}/web-common/web-common-enums/pom.xml'.

Secondly, execute

mvn install

on web-common-enums module.

Finally, execute

mvn compile

on ref-model module. It worked.

dreamt
  • 3
  • 1
  • right, But this is just a simple demo, in my production situation I have some dependencies from it's parent `web-common`. And I have found that maven is not always failed to build in my situation, sometimes it will succeed. – william Jan 28 '22 at 07:47
  • And it also will succeed by several times with this demo. I don't know if it is related to my package path. e.g. classes was from the same package name but in different module. – william Jan 28 '22 at 07:50
0

OMG! just remove all the plugins in web-common and it's child modules.....

william
  • 265
  • 4
  • 17