您可以pom.xml
像我在此函数中一样读取并保存您的任何属性:
/**
* Save a property in a pom.xml
*
* @param propertyName Name of the property
* @param value New value for the property
*/
public static void saveProjectProperty(String propertyName, String value)
{
Model model = null;
FileReader reader = null;
MavenXpp3Reader mavenreader = new MavenXpp3Reader();
try
{
reader = new FileReader("pom.xml");
model = mavenreader.read(reader);
MavenProject project = new MavenProject(model);
while (project.getParent() != null)
{
project = project.getParent();
}
project.getProperties().put(propertyName, value);
try (FileWriter fileWriter = new FileWriter("pom.xml"))
{
project.writeModel(fileWriter);
}
}
catch (IOException ex)
{
LOG.severe("Error saving pom.xml");
}
catch (XmlPullParserException ex)
{
LOG.warning("Error reading pom.xml");
}
}
为了能够在 JVM 中使用非本机类,您应该添加以下依赖项:
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>2.0</version>
</dependency