Java
在本示例中,您将创建一个 mavenDependencies
蓝图,该蓝图将通过 Port'sAPI 和webhook functionality 的组合来引用 Maven 软件包。
要将 Maven 依赖包引用到 Port,需要使用一个脚本,该脚本会根据 webhook 配置发送有关软件包的信息。
先决条件
创建以下蓝图定义和 webhook 配置:
Maven dependency blueprint
{
"identifier": "mavenDependency",
"description": "This blueprint represents a Maven dependency",
"title": "Maven Dependency",
"icon": "java",
"schema": {
"properties": {
"groupId": {
"icon": "DefaultProperty",
"title": "Group ID",
"description": "The group ID of the dependency package",
"type": "string"
},
"artifactId": {
"type": "string",
"title": "Artifact ID",
"description": "The artifact ID of the dependency package"
},
"version": {
"type": "string",
"title": "Version",
"description": "The version of the dependency"
},
"scope": {
"type": "string",
"title": "Scope",
"description": "The scope of the dependency (e.g., compile, test, etc.)"
}
},
"required": []
},
"mirrorProperties": {},
"calculationProperties": {},
"relations": {}
}
Maven webhook configuration
{
"identifier": "mavenDependencyMapper",
"title": "Maven Dependency Mapper",
"description": "A webhook configuration to map Maven dependencies to Port",
"icon": "Java",
"mappings": [
{
"blueprint": "mavenDependency",
"entity": {
"identifier": ".body.artifactId",
"title": ".body.groupId",
"properties": {
"groupId": ".body.groupId",
"artifactId": ".body.artifactId",
"version": ".body.version",
"scope": ".body.scope"
}
}
}
],
"enabled": true
}
Maven Bash script
#!/bin/bash
set -e
# Create or clear the output file
echo "[]" > output.json
# Extract dependencies from pom.xml
mapfile -t dependencies < <(xmlstarlet sel -N x=http://maven.apache.org/POM/4.0.0 -t -m '//x:dependency' -v 'concat(x:groupId, ":", x:artifactId, ":", x:version, ":", x:scope)' -n pom.xml)
# Parse each dependency into a package JSON
for dependency in "${dependencies[@]}"; do
# Split line into an array
IFS=':' read -r -a parts <<< "$dependency"
# Assign array items to variables
groupId="${parts[0]}"
artifactId="${parts[1]}"
version="${parts[2]}"
scope="${parts[3]}"
# Create the package JSON
package_json=$(jq -n \
--arg gi "$groupId" \
--arg ai "$artifactId" \
--arg v "$version" \
--arg s "$scope" \
'{
groupId: $gi,
artifactId: $ai,
version: $v,
scope: $s
}')
# Add the package JSON to the output file
jq --argjson p "$package_json" '. += [$p]' output.json > temp.json && mv temp.json output.json
# Send the package JSON to the webhook
curl --location 'https://ingest.getport.io/YOUR_WEBHOOK_URL' \
--header 'Content-Type: application/json' \
--data "$package_json"
done
note
- The script utilizes the
mapfile
command, which is a built-in command in the Bash shell, to read lines from thepom.xml
file and store them in an array. Please note that this command may not be available in all shells by default. If you are using a different shell such as Dash or Zsh, you may need to switch to Bash or modify the script to achieve a similar functionality. - The script relies on the
jq
command for manipulating JSON data. It is used to create JSON objects based on the package details extracted from thego.mod
file and append these objects to an output JSON file. It is important to note thatjq
is a powerful JSON processor for the command-line, but it is not typically included in many systems by default. You may need to install it separately to use it.
解析 pom.xml
文件并向 Port 发送依赖关系数据
下文将概述如何使用映射器脚本将数据从 pom.xml
文件发送到 Port。
脚本 Usage
-
将脚本复制到 Java 项目根目录下的文件中。确保您的
pom.xml
文件也位于项目根目录下; -
使脚本可执行。例如,如果将脚本命名为
parse_and_send.sh
,则应使用以下命令:chmod +x parse_and_send.sh
3.运行脚本:
./parse_and_send.sh
完成!脚本运行后,将自动通过 HTTP 请求将 Maven 依赖项注入 Port