Unable to build adapters using profiles and proper

2019-04-13 03:05发布

问题:

I have created an http adapter using mfpdev adapter create but I found out the adatper.xml configuration file contains server connection configuration which I would like to change accordingly to dev,test,produciton environment.

So I tried to use maven resource filter by changing the pom.xml as following

pom.xml

<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.my.adapter</groupId>
<artifactId>MYAPIAdapter</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>adapter</packaging>
<name>MYAPIAdapter</name>

<properties>
    <!-- Use UTF-8 as the encoding of the adapter -->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <!-- MobileFirst adapter deployment properties -->
    <mfpfUrl>http://localhost:9080/mfpadmin</mfpfUrl>
    <mfpfUser>admin</mfpfUser>
    <mfpfPassword>admin</mfpfPassword>
    <mfpfRuntime>mfp</mfpfRuntime>
</properties>

<dependencies>
    <dependency>
        <groupId>com.ibm.mfp</groupId>
        <artifactId>adapter-maven-api</artifactId>
        <scope>provided</scope>
        <version>[8.0.0,9.0.0)</version>
    </dependency>
    <dependency>
        <groupId>com.ibm.mfp</groupId>
        <artifactId>mfp-security-checks-base</artifactId>
        <version>[8.0.0,9.0.0)</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>com.ibm.mfp</groupId>
            <artifactId>adapter-maven-plugin</artifactId>
            <extensions>true</extensions>
        </plugin>
    </plugins>
    <filters>
        <filter>profiles/${build.profile.id}/config.properties</filter>
    </filters>
    <resources>
        <resource>
            <directory>src/main/adapter-resources</directory>
            <filtering>true</filtering>
        </resource>
</resources>
</build>

<profiles>
    <profile>
        <id>pc</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <build.profile.id>pc</build.profile.id>
        </properties>
    </profile>
    <profile>
        <id>sit</id>
        <properties>
            <build.profile.id>sit</build.profile.id>
        </properties>
    </profile>
    <profile>
        <id>uat</id>
        <properties>
            <build.profile.id>uat</build.profile.id>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <build.profile.id>prod</build.profile.id>
        </properties>
    </profile>
</profiles>

adapter.xml

<mfp:adapter name="MYAPIAdapter"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:mfp="http://www.ibm.com/mfp/integration"
         xmlns:http="http://www.ibm.com/mfp/integration/http">

<displayName>MYAPIAdapter</displayName>
<description>MYAPIAdapter</description>
<connectivity>
    <connectionPolicy xsi:type="http:HTTPConnectionPolicyType">
        <protocol> ${connect.protocol}</protocol>
        <domain> ${connect.domain}</domain>
        <port> ${connect.port}</port>
        <connectionTimeoutInMilliseconds> ${connect.timeout.ms}</connectionTimeoutInMilliseconds>
        <socketTimeoutInMilliseconds> ${connect.sockettimeout.ms}</socketTimeoutInMilliseconds>
        <maxConcurrentConnectionsPerNode> ${max.concurrentconnections}</maxConcurrentConnectionsPerNode>
    </connectionPolicy>
</connectivity>

<procedure name="getFeed"/>
<procedure name="unprotected" secured="false"/>

When I run mvn install , I hit following error , seem like mfp maven plugin validate the adapter xml file. I have no problem running with mvn compile and the adapter xml values is succesfully replaced with the values in properties file. Is there anyway to specify the configuration from properties file when building the mfp adapter?

ERROR] Failed to execute goal com.ibm.mfp:adapter-maven-plugin:8.0.2016082422:build (default-build) on project MYAPIAdapter: Adapter xml file schema validation encountered errors: [cvc-datatype-valid.1.2.1: '${connect.port}' is not a valid value for 'integer'., cvc-type.3.1.3: The value '${connect.port}' of element 'port' is not valid., cvc-datatype-valid.1.2.1: '${connect.timeout.ms}' is not a valid value for 'integer'., cvc-type.3.1.3: The value '${connect.timeout.ms}' of element 'connectionTimeoutInMilliseconds' is not valid., cvc-datatype-valid.1.2.1: '${connect.sockettimeout.ms}' is not a valid value for 'integer'., cvc-type.3.1.3: The value '${connect.sockettimeout.ms}' of element 'socketTimeoutInMilliseconds' is not valid., cvc-datatype-valid.1.2.1: '${max.concurrentconnections}' is not a valid value for 'integer'., cvc-type.3.1.3: The value '${max.concurrentconnections}' of element 'maxConcurrentConnectionsPerNode' is not valid.] -> [Help 1] [ERROR] 

回答1:

In MobileFirst Foundation 8.0 you can use either the MobileFirst CLI or Maven to update the server with different "profiles". See here: https://mobilefirstplatform.ibmcloud.com/tutorials/en/foundation/8.0/adapters/javascript-adapters/#pull-and-push-configurations

CLI:

  • mfpdev adapter pull
  • mfpdev adapter push

Maven:

  • mvn adapter:configpull -DmfpfConfigFile=config.json
  • mvn adapter:configpush -DmfpfConfigFile=config.json

After you deploy your adapter for the first time, use the mfpdev adapter pull command. Because you did not alter any value via the MobileFirst Operations Console just yet, this will create an empty config.json file at the root of the adapter directory (i.e. JavaScriptHTTP\config.json):

{ }

You can now edit this config file for a specific "profile", and then duplicate it for each of the other "profiles".

To edit it more easily, it is better to first edit the various properties in the console first and then pull them, in order to created a populated config.json file. For example, after changing the domain, port and protocol values, saving the changes and running the pull command, the config.json file will be populated with the following:

{"connectivity":{"http":{"protocol":{"value":"http"},"port":{"value":4431},"domain":{"value":"ibmcloud.com"}}}}

You can beautify it to:

{
    "connectivity": {
        "http": {
            "protocol": {
                "value": "http"
            },
            "port": {
                "value": 4431
            },
            "domain": {
                "value": "ibmcloud.com"
            }
        }
    }
}

Now duplicate this file for each "profile" and customize the values.
You can then push it back to the server.

  1. Deploy the adapter as-is (doesn't matter what are its default properties in the adapter.xml since these will be overwritten based on the config file you'll push).

  2. Push the specific config.json file: mfpdev adapter push.

Learn more here: https://mobilefirstplatform.ibmcloud.com/blog/2017/01/03/tools-for-devops-flows-with-mobilefirst-foundation/

If you need to store different files, such as: dev.json, qa.json, uat.json and prod.json, you can still do this, however instead of the CLI - Use Maven:

mvn adapter:configpush -DmfpfConfigFile=config.json

Replace "config.json" with the name of the appropriate .json file.

Alternatively, continue using your current implementation and by using mvn compile. You can also use Maven command to build and deploy the adapter: https://mobilefirstplatform.ibmcloud.com/tutorials/en/foundation/8.0/adapters/creating-adapters/



回答2:

As a workaround you can set the path to your generated adapter.xml in pom.xml.

```
<build>
<resources>
  <resource>
    <directory>src/main/adapter-resources</directory>
    <filtering>true</filtering>
    <targetPath>${adapterResourcePath}</targetPath>
  </resource>
  <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
  </resource>
</resources>
<plugins>
  <plugin>
    <groupId>com.ibm.mfp</groupId>
    <artifactId>adapter-maven-plugin</artifactId>
    <version>${version.mffadapter.plugin}</version>
    <extensions>true</extensions>
    <executions>
      <execution>
        <goals>
          <goal>build</goal>
        </goals>
        <configuration combine.self="override">
          <adapterResourcesDir implementation="java.io.File">${adapterResourcePath}
          </adapterResourcesDir>
        </configuration>
      </execution>
    </executions>
  </plugin>