【问题标题】:How can I use properties exactly as they are provided by the io.fabric8 docker-maven-plugin?如何完全按照 io.fabric8 docker-maven-plugin 提供的属性使用它们?
【发布时间】:2016-08-03 13:28:14
【问题描述】:

io.fabric8 docker-maven-plugin 填充的 Maven 属性在按原样使用时似乎没有被插值。

The docker-maven-plugin fills in some maven propertiessome.hostsome.port)我尝试解决。

<plugin>
    <groupId>io.fabric8</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>0.15.5</version>
    <configuration>
        <images>
            <image>
                <alias>...</alias>
                <name>...</name>
                <run>
                    <ports>
                        <port>+some.host:some.port:5432</port>
                    </ports>
                    <namingStrategy>alias</namingStrategy>
                </run>
            </image>
        </images>
    </configuration>
</plugin>

它们是这样使用的:

<properties>
    <docker.host>${some.host}</docker.host>
    <docker.port>${some.port}</docker.port>
</properties>

导致两个空值。它们不包含任何内容,而它们应该包含例如127.0.0.15555


如果我添加一些字符,突然间值会被正确插入(当然这些值是无用的)

<properties>
    <docker.host>${some.host}+abc</docker.host>
    <docker.port>${some.port}+123</docker.port>
</properties>

导致127.0.0.1+abc5555+123


我尝试过的一些方法也不起作用:

<properties>
    <dollar>$</dollar>
    <docker.host>${dollar}{some.host}</docker.host>
    <docker.port>${some.port}${}</docker.port>
</properties>

导致空值和5555null

【问题讨论】:

  • 你在哪里使用这些属性?映射属性和implicit set properties 仅在插件启动之后,即在docker:start 目标运行之后。你能提供一个示例项目吗?
  • 我在设置这些属性的同一阶段使用这些属性,因为一个容器需要另一个容器的值。因此,它不起作用似乎是合乎逻辑的,但在过去它确实起作用(因为我们首先将主机和端口放在一起)我们只是开始怀疑这种新情况是什么时候出现的,我们分别需要这些值。打破了我们的头脑......我无法提供示例项目,因为它使用我们自己的图像,我们不允许公开。但我猜任何 docker 镜像都会导致同样的问题。
  • 我们现在有一个解决方法,但它有点小技巧,所以我还在等着看这里是否有人有任何想法。
  • 除了这个问题,我很快就会看到(顺便说一句,你能不能在github.com/fabric8io/docker-maven-plugin打开一个问题,因为我们可以在那里更好地跟踪),你考虑过使用@ 987654324@ 将您的容器连接在一起?这样您就不需要端口映射并且可以使用固定端口。
  • 你的提议很好。使用 docker 容器作为主机及其固定的内部 docker 端口消除了拥有 maven 属性的需要。实际上,只需在需要使用它的容器中添加一个 即可完成。而且我现在认为我的问题没有任何无法以更好的方式解决的实际用例。所以也许不需要问题。如果您将建议添加为完整答案,我会接受。感谢您的见解。

标签: maven properties fabric8


【解决方案1】:

Roland Huß 注意到使用 docker 固定端口可以避免该问题。这应该是首选。

但为了完成,我将添加一个同样有效的解决方案:

  1. 欺骗 maven 创建一个空属性(例如,使用 groovy-maven-plugin)

        <plugin>
            <groupId>org.codehaus.gmaven</groupId>
            <artifactId>groovy-maven-plugin</artifactId>
            <version>2.0</version>
            <executions>
                <execution>
                    <id>set empty property</id>
                    <phase>initialize</phase>
                    <goals>
                        <goal>execute</goal>
                    </goals>
                    <configuration>
                        <source>
                                <![CDATA[
                            project.properties.setProperty('empty', '');
                            ]]>
                            </source>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    
  2. 使用空属性让maven对docker-maven-plugin暴露的属性进行插值

    <docker.host>${some.host}${empty}</docker.host>
    <docker.port>${some.port}${empty}</docker.port>
    

【讨论】:

    猜你喜欢
    • 2019-07-02
    • 1970-01-01
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    • 2015-09-03
    • 2019-08-05
    • 2015-02-21
    • 1970-01-01
    相关资源
    最近更新 更多