user26270
user26270

Reputation: 7084

how do you access azure pipelines variables in a java maven project?

We have an existing java app in an azure pipline which we're trying to add using variables in. I have mostly a Java background and am pretty new to ADO and azure pipelines. I've read the docs on azure pipeline variables but don't see how to get these variables in the java app. The docs say that the variables can be used as input to a task, and also are made available to scripts through environment variables.

This project is a maven project; the first task is maven 'clean install', so is there a way to inject the ADO pipeline variable into the maven build, and then somehow have maven turn it into a System property or environment variable which the program can access via System.getPropert() or System.genenv()?

Upvotes: 0

Views: 6933

Answers (2)

Levi Lu-MSFT
Levi Lu-MSFT

Reputation: 30333

You can add a bash task to expose your pipeline variables as environment variables for current shell. Please Refer to below example.

I create a java maven test project with below java code. I tested and found below code cannot access to the variable I defined in pipeline variable.

enter image description here

The workaround is to add bash task to set the environment variable on the agent. For below example.

export MyTestVariable="$(MyTestVariable)"
export TestProperty="$(TestProperty)"

above script expose two env variables for current shell. Youc can refer to this thread

Then run mvn command from bash command. Below is my full script.

enter image description here

Then from the test output log. The variable can be accessed from the java project by using method System.genenv().

enter image description here

Upvotes: 1

Hugh Lin
Hugh Lin

Reputation: 19401

When a pipeline executes, Azure will place all pipeline variables into environment variables, so any tools, scripts, tasks, or processes you run as part of the build can access parameters through the environment.

You can define a variable in a pipeline YAML definition or in the DevOps pipeline GUI.

To use a variable as an input to a task, wrap it in $(). The syntax for using these environment variables depends on the scripting language. Name is upper-cased, . replaced with _, and automatically inserted into the process environment. This is stated in this part of docs.

This blog could help you ,please refer to it.

Upvotes: 2

Related Questions