一個Docker的maven插件
一個Docker的maven插件的簡單介紹。原文地址:http://www.ofbizian.com/2014/04/a-docker-maven-plugin-for-integration.html
什么是Docker
Docker最近在業內非常火。如果你現在還不知道Docker是什么,你可要小心嘍。今后,你會發現自己正在以某種方式使用它。本文假設你已經有了Docker的基礎。如果你現在對它還不是很熟悉,我確定你以后還會來讀這篇文章。
Docker用于集成測試、復雜分布式系統演示,非常理想。甚至可以用于運行生產環境下的系統。它是一個開源的軟件容器。你可以把它想像成一個非常輕的超級快的虛擬機。
例子
得到“Integration testing with Maven and Docker”文章和Docker Java API項目的啟發,我寫了一個簡單的可以管理Docker容器maven插件,Docker Maven Plugin。這個插件將會根據你的配置,在構建時啟動容器,構建結束時停止容器并刪除,如果本地找不到鏡像,Docker會自動去中央倉庫下載。
以下與Apache Camel的集成測試是被忽略的,因為測試需要一個Redis實例才可以執行:
package org.apache.camel.component.redis;import org.apache.camel.impl.JndiRegistry; import org.junit.Ignore; import org.junit.Test; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; @Ignore public class RedisProducerIntegrationTest extends RedisTestSupport { private static final JedisConnectionFactory CONNECTION_FACTORY = new JedisConnectionFactory(); static { CONNECTION_FACTORY.afterPropertiesSet(); } @Override protected JndiRegistry createRegistry() throws Exception { JndiRegistry registry = super.createRegistry(); redisTemplate = new RedisTemplate(); redisTemplate.setConnectionFactory(CONNECTION_FACTORY); redisTemplate.afterPropertiesSet(); registry.bind("redisTemplate", redisTemplate); return registry; } @Test public void shouldSetAString() throws Exception { sendHeaders( RedisConstants.COMMAND, "SET", RedisConstants.KEY, "key1", RedisConstants.VALUE, "value"); assertEquals("value", redisTemplate.opsForValue().get("key1")); } @Test public void shouldGetAString() throws Exception { redisTemplate.opsForValue().set("key2", "value"); Object result = sendHeaders(RedisConstants.KEY, "key2", RedisConstants.COMMAND, "GET"); assertEquals("value", result); } }</pre><br />
我們配置docker-maven-plugin使用一個Redis鏡像同時讓主機的6379端口映射到容器的6379端口:
<plugin> <groupId>com.ofbizian</groupId> <artifactId>docker-maven-plugin</artifactId> <version>1.0.0</version> <configuration> <images> <image> <name>dockerfile/redis</name> <hostConfig> <![CDATA[ { "PortBindings": { "6379/tcp": [ { "HostIp": "0.0.0.0", "HostPort": "6379" } ] } } ]]> </hostConfig> </image> </images> </configuration> <executions> <execution> <id>start-docker</id> <phase>pre-integration-test</phase> <goals> <goal>start</goal> </goals> </execution> <execution> <id>stop-docker</id> <phase>post-integration-test</phase> <goals> <goal>stop</goal> </goals> </execution> </executions> </plugin>
插件在編譯期啟動一個Docker容器,然后在集成測試結束期關閉容器。
這是一個非常簡單的例子,但是這個插件支持更多的場景,如多鏡像不同配置;將啟動/關閉容器動作定義在不同的maven構建期間。Enjoy.
</span> </span>
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!