maven 配置eureka服务端多环境

本文详细介绍了如何在Maven项目中正确配置变量替换,以实现不同环境下的资源文件自动加载,并展示了如何搭建SpringBoot的Eureka Server服务注册中心,包括依赖管理、插件配置及构建过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

配置文件:

pom文件:

此处需注意:网上很多对maven变量替换的描述都不全,如果要开启maven变量替换必须将

filtering和useDefaultDelimiters同时设置为true方可生效
<?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">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.cyh</groupId>
  <artifactId>ums.eureka.server</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>ums.eureka.server</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>
  <parent>
      <!--引入spring boot 父项目-->
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.0.0.RELEASE</version>
  </parent>
  <dependencies>
      <dependency>
              <groupId>org.springframework.cloud</groupId>
              <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
      </dependency>
  </dependencies>
  <dependencyManagement>
      <dependencies>
          <!--引入spring cloud 依赖管理-->
          <dependency>
              <groupId>org.springframework.cloud</groupId>
              <artifactId>spring-cloud-dependencies</artifactId>
              <version>Finchley.RELEASE</version>
              <type>pom</type>
              <scope>import</scope>
          </dependency>
      </dependencies>
  </dependencyManagement>
  <profiles>
      <profile>
          <id>dev</id>
          <!--激活条件-->
          <activation>
              <!--默认激活-->
              <activeByDefault>true</activeByDefault>
          </activation>
          <properties>
              <profiles.active>dev</profiles.active>
          </properties>

      </profile>
      <profile>
          <id>test</id>
          <properties>
              <profiles.active>test</profiles.active>
          </properties>
      </profile>
      <profile>
          <id>pro</id>
          <properties>
              <profiles.active>pro</profiles.active>
          </properties>
      </profile>
  </profiles>
  
  <build>
    <filters>
        <filter>src/main/resources/application-${profiles.active}.properties</filter>
    </filters>
    <resources>
        <resource>
            <!--打包排除文件-->
            <excludes>
                <exclude>application-dev.properties</exclude>
                <exclude>application-test.properties</exclude>
                <exclude>application-pro.properties</exclude>
            </excludes>
            <directory>src/main/resources</directory>
            <!--打包要复制的文件-->
            <includes>
                <include>application.properties</include>
            </includes>
            <!--开启变量替换-->
            <filtering>true</filtering>
        </resource>
    </resources>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.1.0</version>
          <configuration>
              <!--开启变量替换-->
              <useDefaultDelimiters>true</useDefaultDelimiters>
          </configuration>
        </plugin>
          <!--maven运行springboot插件-->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>com.cyh.App</mainClass>
            </configuration>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.7.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.20.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

运行

在此处我们还可以将springboot打为war包,引入tomcat7-maven-plugin插件运行maven命令一键部署。

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.1</version>
    <configuration>
        <!-- 注意tomcat7此处的url -->
        <url>http://localhost:8080/manager/text</url>
        <server>tomcat7</server> <!-- 此处的名字必须和setting.xml中配置的ID一致-->
        <path>/</path> <!-- 此处的名字是项目发布的工程名-->
    </configuration>
</plugin>

如果服务器比较多可以用Jenkins+docker的方式部署

转载于:https://my.oschina.net/u/3484671/blog/2876240

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值