Skip to content

Commit

Permalink
ID-32: generate 'gateway.info.json' file for server and deployment ar…
Browse files Browse the repository at this point in the history
…chives
  • Loading branch information
mlookaxw committed Dec 17, 2019
1 parent 18c5f18 commit f4bb09f
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.settings
.data
.policystudio
.studio
.vscode
.projdeps.json
*.class
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ Example:
$ buildfed ... -F description:config/description.txt

This will define a new property `description` which is populated from the content of the `description.txt` file.

|https://github.com/Axway-API-Management-Plus/apigw-maven-plugin/issues/32[#32]
|Enhancement
|Build artifact information JSON.

For server and deployment archives the plugin will generate a `gateway.info.json` file.
The file contains information about the version of the artifact and its dependencies.

On configuring the `.fed` package, the plugin passes the content of the `gateway.info.json` file as `_system.artifact.info` property to the `buildfed` tool.
This enables the developer to use the contained JSON document to build an endpoint providing information about the currently deployed version.
|===

== Version 0.10.0
Expand Down
5 changes: 5 additions & 0 deletions doc/manual/_config-tool.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ The plugin and the configuration tool provides a set of predefined system proper
|Complete ID of the project artifact.
|Plugin

|_system.artifact.info
|JSON describing the artifact and its dependencies (content of the `gateway.info.json` file).
If the `gateway.info.json` file is not available the property contains an empty JSON document `{}`.
|Plugin

|_system.build.datetime
|Build date and time in ISO format (YYYY-MM-DD'T'HH:MM:SS.S)
|Config Tool
Expand Down
76 changes: 65 additions & 11 deletions doc/manual/_reference.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ icon:folder[] server-archive.axsar <1>
icon:folder[] staticFiles <3>
icon:file[] gateway.pol <4>
icon:file[] gateway.env <5>
icon:file[] readme-server-archive.txt <6>
icon:file[] gateway.info.json <6>
icon:file[] readme-server-archive.txt <7>
----
<1> Generated server archive.
<2> Folder containing dependent JARs.
Expand All @@ -108,7 +109,30 @@ The files are copied from the `staticFiles` folders of all dependent policy arch
<4> Packed `.pol` archive containing the server configuration and polices (including policies of all dependent policy archives)
<5> `.env` file containing all environment specific information.
The environment is not configured and should be treated as environment independent.
<6> Generated readme file describing the content of the archive (e.g. version of dependent policy archives)
<6> JSON document describing the content of the archive (e.g. version and dependencies)
<7> Generated readme file describing the content of the archive (e.g. version of dependent policy archives)

*`gateway.info.json` File*

The artifact information file describes the content of the server archive as a JSON document.
It contains information about the deployment archive itself the dependencies.

[source,json]
----
{
"id" : "com.example:hello-world-gw:axway-server-archive:0.1.0-SNAPSHOT",
"name" : "Hello World Server",
"description" : "My first \"Hello World\" server project.",
"buildTime" : "2019-12-17T17:25:40",
"artifact" : {
"groupID" : "com.example",
"artifactID" : "hello-world-gw",
"version" : "0.1.0-SNAPSHOT"
},
"dependencies" : [ "com.example:hello-world:axway-policy-archive:0.1.0-SNAPSHOT" ]
}
----


=== Deployment Project
Deployment projects are used to configure API gateways for specific environments or groups based on an environment independent server project.
Expand Down Expand Up @@ -138,24 +162,54 @@ icon:folder[] deploy-archive.axdar <1>
icon:folder[] lib <2>
icon:folder[] staticFiles <3>
icon:file[] gateway.fed <4>
icon:file[] gateway.pol <5>
icon:file[] gateway.env <6>
icon:file[] readme-deployment-archive.txt <7>
icon:file[] readme-server-archive.txt <8>
icon:file[] gateway.info.json <5>
icon:file[] readme-deployment-archive.txt <6>
icon:file[] readme-server-archive.txt <7>
----
<1> Generated server archive.
<2> Folder containing dependent JARs.
<3> Static files belonging to the gateway instance.
The files are copied from the `staticFiles` folders of all dependent policy archives.
<4> Configured `.fed` file.
It is environment specific and contains all configured certificates and environmentalized fields.
<5> Packed `.pol` archive containing the server configuration and polices (including policies of all dependent policy archives)
<6> Configured `.env` file containing all environment specific information.
All certificates and environmentalized fields are replace by configured values.
<7> Generated readme file describing the content of the archive (e.g. version of depnednt server archive).
<8> Generated readme file describing the content of the archive (e.g. version of dependent policy archives).
<5> JSON document describing the content of the archive (e.g. version and dependencies)
<6> Generated readme file describing the content of the archive (e.g. version of dependent server archive).
<7> Generated readme file describing the content of the archive (e.g. version of dependent policy archives).
Copied from server archive.

*`gateway.info.json` File*

The artifact information file describes the content of the deployment archive as a JSON document.
It contains information about the deployment archive itself and about the based server archive.

[source,json]
----
{
"id" : "com.example:hello-world-dev:axway-deployment-archive:0.1.0-SNAPSHOT",
"name" : "Hello World (DEV)",
"description" : null,
"buildTime" : "2019-12-17T17:25:40",
"artifact" : {
"groupID" : "com.example",
"artifactID" : "hello-world-dev",
"version" : "0.1.0-SNAPSHOT"
},
"dependencies" : [ "com.example:hello-world-gw:axway-server-archive:0.1.0-SNAPSHOT" ],
"serverArchive" : {
"id" : "com.example:hello-world-gw:axway-server-archive:0.1.0-SNAPSHOT",
"name" : "Hello World Server",
"description" : "My first \"Hello World\" server project.",
"buildTime" : "2019-12-17T17:25:40",
"artifact" : {
"groupID" : "com.example",
"artifactID" : "hello-world-gw",
"version" : "0.1.0-SNAPSHOT"
},
"dependencies" : [ "com.example:hello-world:axway-policy-archive:0.1.0-SNAPSHOT" ]
}
}
----

== Build Properties

The plugin can be configured by various properties.
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/com/axway/maven/apigw/AbstractGatewayMojo.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.axway.maven.apigw;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
Expand All @@ -13,6 +15,10 @@
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

/**
*
*/
Expand Down Expand Up @@ -254,4 +260,29 @@ protected List<File> getPropertyFiles() {

return propFiles;
}

protected ObjectNode buildBasicArtifactInfo() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
ObjectMapper mapper = new ObjectMapper();

ObjectNode artifact = mapper.createObjectNode();
artifact.put("groupID", this.project.getGroupId());
artifact.put("artifactID", this.project.getArtifactId());
artifact.put("version", this.project.getVersion());

ObjectNode root = mapper.createObjectNode();
root.put("id", this.project.getId());
root.put("name", this.project.getName());
root.put("description", this.project.getDescription());
root.put("buildTime", df.format(new Date()));
root.set("artifact", artifact);

ArrayNode deps = mapper.createArrayNode();
for (Artifact a : getDependencies(null)) {
deps.add(a.getId());
}
root.set("dependencies", deps);

return root;
}
}
35 changes: 31 additions & 4 deletions src/main/java/com/axway/maven/apigw/DeploymentArchiveMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@
import org.apache.maven.plugins.annotations.ResolutionScope;

import com.axway.maven.apigw.utils.FedBuilder;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

@Mojo(name = "axdar", defaultPhase = LifecyclePhase.PACKAGE, requiresProject = true, threadSafe = false, requiresDependencyResolution = ResolutionScope.COMPILE, requiresDependencyCollection = ResolutionScope.COMPILE)
public class DeploymentArchiveMojo extends AbstractFlattendProjectArchiveMojo {

public static final String FILE_FED_NAME = "gateway.fed";
public static final String FILE_GATEWAY_INFO = "gateway.info.json";
public static final String FILE_README = "readme-deployment-archive.txt";
public static final String FILE_GATEWAY_CONFIG_JSON = "gateway.config.json";

Expand Down Expand Up @@ -69,12 +72,13 @@ protected List<ArchiveDir> prepareDirs() throws MojoExecutionException {
FileUtils.deleteDirectory(archiveBuildDir);
archiveBuildDir.mkdirs();

buildFedArchive(archiveBuildDir, srcPolFile, srcEnvFile);
File infoFile = prepareInfoJson(archiveBuildDir);
buildFedArchive(archiveBuildDir, srcPolFile, srcEnvFile, infoFile);

prepareReadme(archiveBuildDir);
FileUtils.copyFileToDirectory(
new File(getSharedArtifactDir(this.serverArchive), ServerArchiveMojo.FILE_README), archiveBuildDir);

prepareStaticFiles(new File(archiveBuildDir, DIR_STATIC_FILES));
prepareJars(new File(archiveBuildDir, ServerArchiveMojo.DIR_LIBS));

Expand All @@ -100,6 +104,29 @@ private void prepareReadme(File targetDir) throws IOException, MojoExecutionExce
FileUtils.writeStringToFile(readme, str.toString(), "UTF-8");
}

private File prepareInfoJson(File targetDir) throws IOException, MojoExecutionException {
ObjectMapper mapper = new ObjectMapper();
File info = new File(targetDir, FILE_GATEWAY_INFO);

ObjectNode root = buildBasicArtifactInfo();

ObjectNode serverJson;
File serverInfo = new File(getSharedArtifactDir(this.serverArchive), ServerArchiveMojo.FILE_GATEWAY_INFO);
if (serverInfo.canRead()) {
serverJson = (ObjectNode) mapper.readTree(serverInfo);
} else {
serverJson = mapper.createObjectNode();
serverJson.put("id", this.serverArchive.getId());
}
root.set("serverArchive", serverJson);

String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(root);

FileUtils.writeStringToFile(info, json, "UTF-8");

return info;
}

private void prepareStaticFiles(File targetDir) throws IOException, MojoExecutionException {
File staticFiles;

Expand All @@ -124,15 +151,15 @@ private void prepareJars(File targetDir) throws IOException, MojoExecutionExcept
}
}

private void buildFedArchive(File targetDir, File srcPolFile, File srcEnvFile) throws MojoExecutionException {
private void buildFedArchive(File targetDir, File srcPolFile, File srcEnvFile, File infoFile) throws MojoExecutionException {
File configFile = this.configConfigFile;
if (configFile == null) {
configFile = new File(this.sourceDirectory, FILE_GATEWAY_CONFIG_JSON);
}

File outFedFile = new File(targetDir, FILE_FED_NAME);

FedBuilder fedBuilder = new FedBuilder(this, srcPolFile, srcEnvFile, configFile);
FedBuilder fedBuilder = new FedBuilder(this, srcPolFile, srcEnvFile, configFile, infoFile);

fedBuilder.addPropertyFiles(getPropertyFiles());

Expand Down
18 changes: 14 additions & 4 deletions src/main/java/com/axway/maven/apigw/DeploymentMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Execute;
Expand All @@ -15,6 +16,8 @@
import com.axway.maven.apigw.utils.FedBuilder;
import com.axway.maven.apigw.utils.ProjectDeploy;
import com.axway.maven.apigw.utils.ProjectPack;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

@Mojo(name = "deploy", defaultPhase = LifecyclePhase.NONE, requiresProject = true, threadSafe = false)
@Execute(phase = LifecyclePhase.PACKAGE)
Expand Down Expand Up @@ -84,8 +87,14 @@ private void deployPolicyProject() throws MojoExecutionException {
// configure fed
File pol = new File(getTempDir(), PROJECT_NAME + ".pol");
File env = new File(getTempDir(), PROJECT_NAME + ".env");
File info = new File(getTempDir(), PROJECT_NAME + ".info.json");

File fed = configFed(pol, env);
ObjectMapper mapper = new ObjectMapper();
ObjectNode root = buildBasicArtifactInfo();
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(root);
FileUtils.writeStringToFile(info, json, "UTF-8");

File fed = configFed(pol, env, info);

// deploy to server
deployFed(fed);
Expand All @@ -100,8 +109,9 @@ private void deployServerProject() throws MojoExecutionException {
// configure fed
File pol = new File(archiveBuildDir, ServerArchiveMojo.FILE_GATEWAY_POL);
File env = new File(archiveBuildDir, ServerArchiveMojo.FILE_GATEWAY_ENV);
File info = new File(archiveBuildDir, ServerArchiveMojo.FILE_GATEWAY_INFO);

File fed = configFed(pol, env);
File fed = configFed(pol, env, info);

// deploy to server
deployFed(fed);
Expand All @@ -113,8 +123,8 @@ private void deployDeploymentProject() throws MojoExecutionException {
deployFed(fed);
}

private File configFed(File pol, File env) throws MojoExecutionException {
FedBuilder fb = new FedBuilder(this, pol, env, this.configConfigFile);
private File configFed(File pol, File env, File info) throws MojoExecutionException {
FedBuilder fb = new FedBuilder(this, pol, env, this.configConfigFile, info);
fb.setPassphrasePol(this.passphrasePol);
fb.setPassphraseFed(this.passphraseFed);

Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/axway/maven/apigw/ServerArchiveMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import org.apache.maven.plugins.annotations.ResolutionScope;

import com.axway.maven.apigw.utils.ProjectPack;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

@Mojo(name = "axsar", defaultPhase = LifecyclePhase.PACKAGE, requiresProject = true, threadSafe = false, requiresDependencyResolution = ResolutionScope.COMPILE, requiresDependencyCollection = ResolutionScope.COMPILE)
public class ServerArchiveMojo extends AbstractFlattendProjectArchiveMojo {
Expand All @@ -26,6 +28,7 @@ public class ServerArchiveMojo extends AbstractFlattendProjectArchiveMojo {
public static final String FILE_GATEWAY = "gateway";
public static final String FILE_GATEWAY_POL = FILE_GATEWAY + ".pol";
public static final String FILE_GATEWAY_ENV = FILE_GATEWAY + ".env";
public static final String FILE_GATEWAY_INFO = FILE_GATEWAY + ".info.json";
public static final String FILE_README = "readme-server-archive.txt";

@Override
Expand Down Expand Up @@ -54,6 +57,7 @@ protected List<ArchiveDir> prepareDirs() throws MojoExecutionException {
FileUtils.copyFileToDirectory(envFile, archiveBuildDir);

prepareReadme(archiveBuildDir);
prepareInfoJson(archiveBuildDir);
prepareStaticFiles(new File(archiveBuildDir, DIR_STATIC_FILES));
prepareJars(new File(archiveBuildDir, DIR_LIBS));

Expand All @@ -80,6 +84,17 @@ private void prepareReadme(File targetDir) throws IOException, MojoExecutionExce

FileUtils.writeStringToFile(readme, str.toString(), "UTF-8");
}

private void prepareInfoJson(File targetDir) throws IOException, MojoExecutionException {
ObjectMapper mapper = new ObjectMapper();
File info = new File(targetDir, FILE_GATEWAY_INFO);

ObjectNode root = buildBasicArtifactInfo();

String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(root);

FileUtils.writeStringToFile(info, json, "UTF-8");
}

private void prepareStaticFiles(File targetDir) throws IOException, MojoExecutionException {
File staticFiles;
Expand Down
Loading

0 comments on commit f4bb09f

Please sign in to comment.