如何在开发环境中使用 maven-docker 来避免重复下载 jar?

How to use maven-docker on development-environment to avoid repeated download jars?

这是我的Dockerfile,但是如果我更改了java代码,然后在终端输入"docker-compose up -d --build",maven会重新下载jar,如何避免重复下载?

Docker 文件:

FROM maven:3.5.0-jdk-8
WORKDIR /app
COPY pom.xml .
COPY src ./src
RUN mvn verify
ENTRYPOINT ["java","-jar","target/cloud-user.jar"]

docker-compose.yml:

version: "2"
services:
  cloud-nginx:
    build: cloud-nginx
    image: registry.cn-beijing.aliyuncs.com/wangdali/cloud-nginx
    restart: always
    ports: ["80:80"]
  cloud-consul:
    image: consul:0.9.2
    command: consul agent -dev -client 0.0.0.0
  cloud-front:
    build: cloud-front
    image: registry.cn-beijing.aliyuncs.com/wangdali/cloud-front
    restart: always
    volumes:
      - ./cloud-front/static:/app
  cloud-user:
    build: cloud-user
    image: registry.cn-beijing.aliyuncs.com/wangdali/cloud-user
    restart: always
  mysql: 
    image: mysql:5.7.19
    hostname: mysql
    volumes: 
      - ./docker/mysql/data:/var/lib/mysql 
      - ./docker/mysql/conf:/etc/mysql/conf.d 
      - /etc/localtime:/etc/localtime
    ulimits: 
      nproc: 65535
    mem_limit: 1024m 
    restart: always 
    environment:
      MYSQL_ROOT_PASSWORD: root
    ports:
      - "3306:3306"

我精简了我的测试项目,然后使用spring-maven的-boot插件

mvn spring-boot:run

启动项目,现在先挂载 .m2 文件夹,然后 运行 mvn 命令。 但是我修改了java源码后,devtools并没有自动加载,怎么解决?

这是我的 Dockerfile/docker-compose.yml :

Docker 文件:

version: "2"
services:
  user:
    build: user
    volumes:
      - $HOME/.m2/repository:/root/.m2/repository
      - ./user/src:/app/src
    ports:
      - "8080:8080"

docker-compose.yml:

FROM maven:3.5.0-jdk-8
WORKDIR /app
COPY pom.xml .
COPY src ./src
ENTRYPOINT ["mvn","spring-boot:run"]

解决,我把target文件夹挂载到容器里,可以自动加载,也就是最终的docker文件和docker-compose.yml,当然是生产环境,我们不能写:

Docker 文件:

FROM maven:3.5.0-jdk-8
WORKDIR /app
COPY pom.xml .
COPY src ./src
ENTRYPOINT ["mvn","spring-boot:run"]

docker-compose.yml

version: "2"
services:
  user:
    build: user
    volumes:
      - $HOME/.m2/repository:/root/.m2/repository
      - ./user/target:/app/target
    ports:
      - "8080:8080"
      - "5005:5005"

pom.xml:

    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
          <mainClass>cn.codeforfun.Application</mainClass>
          <jvmArguments>-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005</jvmArguments>
        </configuration>
      </plugin>
    </plugins>