Skip to content

Commit

Permalink
feat(Maven): Add the ability to dynamically set the provider host/por…
Browse files Browse the repository at this point in the history
…t from an expression #1412
  • Loading branch information
Ronald Holshausen committed Aug 29, 2021
1 parent d3f4964 commit d4751be
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
31 changes: 31 additions & 0 deletions provider/maven/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,37 @@ no pact files are loaded after processing all the directories in the list.
</plugin>
```

## Overriding the provider hostname and port when the task is executed (4.2.11+)

Maven supports using expressions in the POM using `${...}`, but these are evaluated when the POM is loaded.

For the provider hostname and port, you can provide expressions of the form `{{...}}` which will be evaluated
using JVM system properties when the verify task is run.

For example:

```xml
<plugin>
<groupId>au.com.dius.pact.provider</groupId>
<artifactId>maven</artifactId>
<version>4.2.11</version>
<configuration>
<serviceProviders>
<serviceProvider>
<name>provider</name>
<host>{{pact.host}}</host>
<port>{{pact.port}}</port>
<pactFileDirectories>
<pactFileDirectory>path/to/pacts</pactFileDirectory>
</pactFileDirectories>
</serviceProvider>
</serviceProviders>
</configuration>
</plugin>
```

This will use `pact.host` and `pact.port` system properties.

## Enabling insecure SSL

For providers that are running on SSL with self-signed certificates, you need to enable insecure SSL mode by setting
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package au.com.dius.pact.provider.maven

import au.com.dius.pact.core.model.FileSource
import au.com.dius.pact.core.pactbroker.ConsumerVersionSelector
import au.com.dius.pact.core.pactbroker.NotFoundHalResponse
import au.com.dius.pact.core.support.expressions.DataType
import au.com.dius.pact.core.support.expressions.ExpressionParser
import au.com.dius.pact.core.support.handleWith
import au.com.dius.pact.core.support.toUrl
import au.com.dius.pact.provider.ConsumerInfo
Expand Down Expand Up @@ -54,6 +57,8 @@ open class PactProviderMojo : PactBaseMojo() {
@Parameter(defaultValue = "console")
lateinit var reports: List<String>

private val expressionParser = ExpressionParser("{{", "}}")

override fun execute() {
systemPropertyVariables.forEach { (property, value) ->
if (value == null) {
Expand Down Expand Up @@ -94,8 +99,22 @@ open class PactProviderMojo : PactBaseMojo() {

try {
val failures = serviceProviders.flatMap { provider ->
provider.host = if (provider.host is String) {
expressionParser.parseExpression(provider.host as String, DataType.RAW)
} else provider.host
provider.port = if (provider.port is String) {
expressionParser.parseExpression(provider.port as String, DataType.RAW)
} else provider.port

val consumers = mutableListOf<IConsumerInfo>()
consumers.addAll(provider.consumers)
consumers.addAll(provider.consumers.map {
if (it.pactSource is String) {
it.pactSource = FileSource(File(it.pactSource as String))
it
} else {
it
}
})
if (provider.pactFileDirectory != null) {
consumers.addAll(loadPactFiles(provider, provider.pactFileDirectory!!))
}
Expand Down

0 comments on commit d4751be

Please sign in to comment.