maven指定配置文件镜像

1.创建dockerFile

FROM bladex/alpine-java:openjdk8-openj9_cn_slim

MAINTAINER [email protected]

RUN mkdir -p /blade

WORKDIR /blade

EXPOSE 8088
EXPOSE 8888
EXPOSE 5005

ADD ./target/full-course-disease-api.jar ./app.jar

ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar","-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005","app.jar"]

CMD ["--spring.profiles.active=prod","-Ddruid.mysql.usePingMethod=false"]

修改pom文件,设置构建插件

  <build>
        <finalName>${bladex.project.id}</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>${spring.boot.version}</version>
                    <configuration>
                        <finalName>${project.build.finalName}</finalName>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>com.spotify</groupId>
                    <artifactId>dockerfile-maven-plugin</artifactId>
                    <version>${docker.plugin.version}</version>
                    <configuration>
                        <username>${docker.username}</username>
                        <password>${docker.password}</password>
                        <repository>${docker.registry.url}/${docker.namespace}/${project.build.finalName}</repository>
                        <tag>${docker.tag}</tag>
                        <!--    <tag>1.0.0-SNAPSHOT</tag>-->
                        <useMavenSettingsForAuth>true</useMavenSettingsForAuth>
                        <!--                 dockerfile文件    -->
                        <dockerfile>${dockerConfigFilePath}</dockerfile>
                        <buildArgs>
                            <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
                        </buildArgs>
                    </configuration>
                    <!--添加如下配置,运行 mvn deploy 命令便会自动打包镜像-->
                    <!--<executions>
                        <execution>
                            <id>default</id>
                            <goals>
                                <goal>build</goal>
                                <goal>push</goal>
                            </goals>
                        </execution>
                    </executions>-->
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.plugin.version}</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>UTF-8</encoding>
                    <compilerArgs>
                        <arg>-parameters</arg>
                    </compilerArgs>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>aliyun-repos</id>
            <url>https://maven.aliyun.com/repository/public/</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>rdc-releases</id>
            <name>Release Repository</name>
            <url>https://packages.aliyun.com/maven/repository/2251575-release-ExJpmm/</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>aliyun-plugin</id>
            <url>https://maven.aliyun.com/repository/public/</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

修改pom文件,设置不同环境启用的配置


    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <profiles.active>dev</profiles.active>
                <docker.tag>${project.version}-SNAPSHOT</docker.tag>
                <dockerConfigFilePath>Dockerfile_test</dockerConfigFilePath>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <profiles.active>test</profiles.active>
                <docker.tag>${project.version}-SNAPSHOT</docker.tag>
                <dockerConfigFilePath>Dockerfile_test</dockerConfigFilePath>
            </properties>
        </profile>
        <profile>
            <!--     灰度 测试环境       -->
            <id>pre</id>
            <properties>
                <profiles.active>pre</profiles.active>
                <docker.tag>${project.version}-SNAPSHOT</docker.tag>
                <dockerConfigFilePath>Dockerfile_test</dockerConfigFilePath>
            </properties>
        </profile>
        <profile>
            <!--     线上       -->
            <id>prod</id>
            <properties>
                <profiles.active>prod</profiles.active>
                <docker.tag>${project.version}.RELEASE</docker.tag>
                <dockerConfigFilePath>Dockerfile_prod</dockerConfigFilePath>
            </properties>
        </profile>
        <profile>
            <!--     nx       -->
            <id>nx</id>
            <properties>
                <profiles.active>nx</profiles.active>
                <docker.tag>${project.version}.nx</docker.tag>
                <dockerConfigFilePath>Dockerfile_nx</dockerConfigFilePath>
            </properties>
        </profile>
    </profiles>

pom配置 镜像服务器地址

    <properties>
        <bladex.project.id>${project.artifactId}</bladex.project.id>
        <bladex.project.version>3.0.1.RELEASE</bladex.project.version>

        <java.version>1.8</java.version>
        <maven.plugin.version>3.8.1</maven.plugin.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <spring.boot.version>2.7.1</spring.boot.version>
        <spring.platform.version>Cairo-SR8</spring.platform.version>

        <!-- 推荐使用Harbor -->
        <docker.registry.url>192.168.2.111:5500</docker.registry.url>
        <docker.username>admin</docker.username>
        <docker.password>Harbor12345</docker.password>
        <docker.namespace>full-course-disease</docker.namespace>
        <docker.plugin.version>1.4.13</docker.plugin.version>
    </properties>

mvn 打包命令

mvn clean package dockerfile:build dockerfile:push -P dev -Dmaven.test.skip=true

猜你喜欢

转载自blog.csdn.net/qq_35385687/article/details/130847257