Azure DevOps Ubuntu构建代理VM添加-javaagent参数及JVM路径查询求助
Hi there, let's walk through how to set this up fully automated without needing manual Tomcat edits or hunting for JVM paths. Here's what you need to do:
1. Securely Retrieve the Application Insights Agent Jar
First, you'll need to get the applicationinsights-agent-3.4.7.jar onto your build agent automatically. Pick the method that fits where your jar is stored:
Option A: Download from a public URL with curl
Add this bash step to your Azure DevOps pipeline:
# Download the agent jar to the agent's temporary directory (cleaned up after pipeline run) curl -o $(Agent.TempDirectory)/applicationinsights-agent-3.4.7.jar https://<your-jar-host-url> # Ensure read permissions for the agent user chmod 644 $(Agent.TempDirectory)/applicationinsights-agent-3.4.7.jar
Option B: Download from Azure Blob Storage (private)
If the jar is in Azure Blob Storage, use the Azure CLI in your pipeline (make sure the service connection has access):
az storage blob download \ --container-name <your-container-name> \ --name applicationinsights-agent-3.4.7.jar \ --file $(Agent.TempDirectory)/applicationinsights-agent-3.4.7.jar \ --account-name <your-storage-account> \ --sas-token <your-sas-token> chmod 644 $(Agent.TempDirectory)/applicationinsights-agent-3.4.7.jar
2. Locate Java's Installation Path (No Manual Hunting!)
You don't need to find specific JVM files—just confirm Java's executable path and home directory with these commands (add them to your pipeline step):
# Get the full path to the Java executable JAVA_EXEC=$(which java) # Derive JAVA_HOME by stripping the /bin/java suffix JAVA_HOME=$(dirname $(dirname $JAVA_EXEC)) echo "Detected Java home directory: $JAVA_HOME"
This gives you a reliable path that works across Ubuntu agents, even if Java is installed via apt or a custom location.
3. Automatically Inject the Java Agent Parameter
Choose the method that matches how you start your application:
If you're starting the app directly with java
Modify your startup command to include the -javaagent flag using the temp directory path:
java -javaagent:"$(Agent.TempDirectory)/applicationinsights-agent-3.4.7.jar" -jar /path/to/your/application.jar
If using Maven (e.g., Spring Boot)
Pass the JVM argument directly to the Maven run command:
mvn spring-boot:run -Dspring-boot.run.jvmArguments="-javaagent:$(Agent.TempDirectory)/applicationinsights-agent-3.4.7.jar"
If using Tomcat (fully automated config edit)
Instead of manual edits, use sed to add the agent to Tomcat's setenv.sh file (create it if it doesn't exist):
# Define the Tomcat bin directory (adjust this to your Tomcat path) TOMCAT_BIN_DIR="/usr/share/tomcat/bin" # Add the javaagent to CATALINA_OPTS in setenv.sh echo 'CATALINA_OPTS="-javaagent:$(Agent.TempDirectory)/applicationinsights-agent-3.4.7.jar $CATALINA_OPTS"' >> $TOMCAT_BIN_DIR/setenv.sh # Make the script executable chmod +x $TOMCAT_BIN_DIR/setenv.sh # Restart Tomcat to apply changes (adjust the service name if needed) sudo systemctl restart tomcat
4. Verify the Agent is Loaded
Add this step to confirm the JVM parameter was applied successfully:
# List all Java processes with their JVM arguments jps -v
Look for your application process—you should see the -javaagent flag in its arguments. You can also check your application logs for Application Insights startup messages to confirm it's working.
内容的提问来源于stack exchange,提问作者eliew

