如何解决maven COMPILATION ERROR:包X不存在?

时间:2017-02-21 16:05:40

标签: java eclipse maven pom.xml spring-tool-suite

我有3个maven项目:

project-A
project-B
project-C

第二个项目(B)需要第一个项目(A)的依赖项。 第三个项目(C)需要第一个和第二个项目(A,B)的依赖项。 我已在相应的项目pom文件中定义了这些依赖项: project-B pom.xml

    <dependency>
        <groupId>com.mygroupid</groupId>
        <artifactId>project-A</artifactId>
        <version>${project.version}</version>
    </dependency>

project-C pom.xml

    <dependency>
        <groupId>com.mygroupid</groupId>
        <artifactId>project-A</artifactId>
        <version>${project.version}</version>
    </dependency>
    <dependency>
        <groupId>com.mygroupid</groupId>
        <artifactId>project-B</artifactId>
        <version>${project.version}</version>
    </dependency>

SpringToolSuite上一切正常。我测试了第二个项目(取决于第一个项目)并测试了第三个项目(这取决于第一个和第二个项目),一切都可以从STS中运行。

当我尝试执行时: mvn clean install -U -DskipTests = true

它适用于project-Aproject-B,但对project-C我得到了:

[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] MyClassOnProjectC.java:[12,38] package XXX does not exist
[ERROR] MyClassOnProjectC.java:[22,17] cannot find symbol
  symbol:   class MyClassOnProjectB
  location: class MyClassOnProjectC

下面是从每个项目中提取的3个类:

项目-A: User.java

package com.neweraed.datamodel;

import java.io.Serializable;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "UserType", propOrder = {
    "name",
    "description",
})
public class User  implements Serializable {    
    private static final long serialVersionUID = -876063526825526098L;

    private String name;
    private String description;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}

项目-B: UserOperations.java

package com.neweraed.services.midpoint;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestOperations;

import com.neweraed.datamodel.User;
import com.neweraed.services.midpoint.utils.Constants;
import com.neweraed.services.midpoint.utils.Utils;

@Component
public class UserOperations {

    private static final Logger logger = LoggerFactory.getLogger(UserOperations.class);

    public User getUser(User user) {
        RestOperations restOperations = Utils.createGenericRestTemplate();
        ResponseEntity<User> response = null;
        try {
            response = restOperations.exchange(Constants.ENDPOIT_SEARCH_USER, HttpMethod.GET, new HttpEntity<Object>(Utils.createHeaders()), User.class);

            logger.info("=== RESPONSE ===" + response + " === ");
        } catch (Exception e) {
            logger.error("=== ERROR === " + e.getMessage() + " === ");
        }
        return user;
    }
}

项目-C: UserService.java

package com.neweraed.rest;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.neweraed.datamodel.User;
import com.neweraed.services.midpoint.UserOperations;

@RestController
@RequestMapping(value="/api/v1/users")
public class UserService {


    @RequestMapping(value="/", method=RequestMethod.POST)
    public User addUser(@RequestBody User user) {
        UserOperations userOperations = new UserOperations();
        return userOperations.addUser(user);
    }
}

我的pom.xml文件似乎一切正常。我怎么解决这个问题?

1 个答案:

答案 0 :(得分:1)

根据您的前提,项目B的pom.xml必须是:

<dependency>
    <groupId>com.mygroupid</groupId>
    <artifactId>project-A</artifactId>
    <version>${project.version}</version>
</dependency>

项目C不需要与A的依赖关系,因为它是在B

上定义的