Skip to content

[#10939] Enable addLocationInformation in DefaultModelXmlFactory base on InputLocationFormatter presence #10940

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.util.function.Function;

import org.apache.maven.model.InputLocation;
import org.apache.maven.model.Model;
import org.apache.maven.model.v4.MavenStaxWriter;

/**
* @deprecated Use MavenStaxWriter instead
* @deprecated Use {@link MavenStaxWriter} instead
*/
@Deprecated
public class MavenXpp3Writer {
Expand All @@ -43,12 +44,10 @@ public class MavenXpp3Writer {
// - Methods -/
// -----------/

public MavenXpp3Writer() {
this(false);
}
public MavenXpp3Writer() {}

protected MavenXpp3Writer(boolean addLocationInformation) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No API breakage in Maven 3 compatibility layer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check. Won’t be able to work on this due to holiday for the next couple weeks. Feel free to adjust it, otherwise I’ll look into this when I’m back.

delegate.setAddLocationInformation(addLocationInformation);
protected MavenXpp3Writer(Function<org.apache.maven.api.model.InputLocation, String> formatter) {
delegate.setStringFormatter(formatter);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@

import org.apache.maven.model.InputLocation;
import org.apache.maven.model.Model;
import org.apache.maven.model.v4.MavenStaxWriter;

/**
* @deprecated Use MavenStaxWriter instead
* @deprecated Use {@link MavenStaxWriter} instead
*/
@Deprecated
public class MavenXpp3WriterEx extends MavenXpp3Writer {
Expand All @@ -36,7 +37,7 @@ public class MavenXpp3WriterEx extends MavenXpp3Writer {
// -----------/

public MavenXpp3WriterEx() {
super(true);
super((inputLocation) -> inputLocation.toString());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Undo

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ void testNamespaceInXmlNode() throws XMLStreamException {
String toXml(Model model) throws IOException, XMLStreamException {
StringWriter sw = new StringWriter();
MavenStaxWriter writer = new MavenStaxWriter();
writer.setAddLocationInformation(false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Undo

writer.write(sw, model);
return sw.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public class SettingsXpp3Writer {

public SettingsXpp3Writer() {
delegate = new SettingsStaxWriter();
delegate.setAddLocationInformation(false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Undo

}
/**
* Method setFileComment.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public class MavenToolchainsXpp3Writer {

public MavenToolchainsXpp3Writer() {
delegate = new MavenToolchainsStaxWriter();
delegate.setAddLocationInformation(false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Undo

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ protected void write(Model model, Path dest) throws IOException, XMLStreamExcept
MavenStaxWriter writer = new MavenStaxWriter();
writer.setNamespace(String.format(NAMESPACE_FORMAT, version));
writer.setSchemaLocation(String.format(SCHEMA_LOCATION_FORMAT, version));
writer.setAddLocationInformation(false);
writer.write(w, model);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
package org.apache.maven.impl;

import java.io.StringReader;
import java.io.StringWriter;

import org.apache.maven.api.model.InputLocation;
import org.apache.maven.api.model.Model;
import org.apache.maven.api.services.xml.XmlReaderException;
import org.apache.maven.api.services.xml.XmlReaderRequest;
import org.apache.maven.api.services.xml.XmlWriterRequest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -40,7 +43,7 @@ void setUp() {
}

@Test
void testValidNamespaceWithModelVersion400() throws Exception {
void testValidNamespaceWithModelVersion400() {
String xml =
"""
<project xmlns="http://maven.apache.org/POM/4.0.0">
Expand All @@ -56,7 +59,7 @@ void testValidNamespaceWithModelVersion400() throws Exception {
}

@Test
void testValidNamespaceWithModelVersion410() throws Exception {
void testValidNamespaceWithModelVersion410() {
String xml =
"""
<project xmlns="http://maven.apache.org/POM/4.1.0">
Expand Down Expand Up @@ -88,7 +91,7 @@ void testInvalidNamespaceWithModelVersion410() {
}

@Test
void testNoNamespaceWithModelVersion400() throws Exception {
void testNoNamespaceWithModelVersion400() {
String xml =
"""
<project>
Expand All @@ -109,7 +112,7 @@ void testNullRequest() {
}

@Test
void testMalformedModelVersion() throws Exception {
void testMalformedModelVersion() {
String xml =
"""
<project xmlns="http://maven.apache.org/POM/4.0.0">
Expand All @@ -122,4 +125,91 @@ void testMalformedModelVersion() throws Exception {
Model model = factory.read(request);
assertEquals("invalid.version", model.getModelVersion());
}

@Test
void testWriteModelWithoutInputLocationTracking() {
String model =
"""
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>example-artifact</artifactId>
<version>1.0.0</version>
<name>Example Project</name>
<properties>
<example.property>value</example.property>
</properties>
</project>""";

String expected =
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>example-artifact</artifactId>
<version>1.0.0</version>
<name>Example Project</name>
<properties>
<example.property>value</example.property>
</properties>
</project>""";

final StringWriter writer = new StringWriter();
XmlWriterRequest<Model> request = XmlWriterRequest.<Model>builder()
.content(factory.read(XmlReaderRequest.builder()
.reader(new StringReader(model))
.build()))
.writer(writer)
.build();

factory.write(request);

final String processedPomAsString = writer.toString();
assertEquals(expected, processedPomAsString);
}

@Test
void testWriteModelWithInputLocationTracking() {
String model =
"""
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>example-artifact</artifactId>
<version>1.0.0</version>
<name>Example Project</name>
<properties>
<example.property>value</example.property>
</properties>
</project>""";

String expected =
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion><!--some formatter: n/a @ 2:3-->
<groupId>org.example</groupId><!--some formatter: n/a @ 3:5-->
<artifactId>example-artifact</artifactId><!--some formatter: n/a @ 4:5-->
<version>1.0.0</version><!--some formatter: n/a @ 5:5-->
<name>Example Project</name><!--some formatter: n/a @ 6:5-->
<properties>
<example.property>value</example.property><!--some formatter: n/a @ 8:32-->
</properties>
</project>""";

final StringWriter writer = new StringWriter();
XmlWriterRequest<Model> request = XmlWriterRequest.<Model>builder()
.content(factory.read(XmlReaderRequest.builder()
.reader(new StringReader(model))
.build()))
.writer(writer)
.inputLocationFormatter((x) -> "some formatter: " + (InputLocation) x)
.build();

factory.write(request);

final String processedPomAsString = writer.toString();
assertEquals(expected, processedPomAsString);
}
}
6 changes: 3 additions & 3 deletions its/run-its.bat
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
@REM In rare occasions, some ITs may depend on latest maven snapshots.
@REM In such cases you need to:
@REM - build maven using `mvn install -PversionlessMavenDist -Dmaven.repo.local=[my-repo-local]`
@REM - run ITs using `mvn clean install -Prun-its,embedded -Dmaven.repo.local=[my-repo-local] -DmavenDistro=[maven-source-tree]/apache-maven/target/maven-bin.zip`
@REM - run ITs using `mvn clean install -Prun-its -Dmaven.repo.local=[my-repo-local] -DmavenDistro=[maven-source-tree]/apache-maven/target/maven-bin.zip`

mvn clean install -U -Prun-its,embedded -Dmaven.repo.local=%cd%\repo
mvn clean install -U -Prun-its -Dmaven.repo.local=%cd%\repo

@REM If behind a proxy try this..
@REM mvn clean install -Prun-its,embedded -Dmaven.repo.local=%cd%\repo -Dproxy.host=<host> -Dproxy.port=<port> -Dproxy.user= -Dproxy.pass= -Dproxy.nonProxyHosts=<hosts>
@REM mvn clean install -Prun-its -Dmaven.repo.local=%cd%\repo -Dproxy.host=<host> -Dproxy.port=<port> -Dproxy.user= -Dproxy.pass= -Dproxy.nonProxyHosts=<hosts>
6 changes: 3 additions & 3 deletions its/run-its.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
# In rare occasions, some ITs may depend on latest maven snapshots.
# In such cases you need to:
# - build maven using `mvn install -PversionlessMavenDist -Dmaven.repo.local=[my-repo-local]`
# - run ITs using `mvn clean install -Prun-its,embedded -Dmaven.repo.local=[my-repo-local] -DmavenDistro=[maven-source-tree]/apache-maven/target/maven-bin.zip`
# - run ITs using `mvn clean install -Prun-its -Dmaven.repo.local=[my-repo-local] -DmavenDistro=[maven-source-tree]/apache-maven/target/maven-bin.zip`

mvn clean install -Prun-its,embedded -Dmaven.repo.local=`pwd`/repo
mvn clean install -Prun-its -Dmaven.repo.local=`pwd`/repo

# If behind a proxy try this

# mvn clean install -Prun-its,embedded -Dmaven.repo.local=`pwd`/repo -Dproxy.host=<host> -Dproxy.port=<port> -Dproxy.user= -Dproxy.pass= -Dproxy.nonProxyHosts=<hosts>
# mvn clean install -Prun-its -Dmaven.repo.local=`pwd`/repo -Dproxy.host=<host> -Dproxy.port=<port> -Dproxy.user= -Dproxy.pass= -Dproxy.nonProxyHosts=<hosts>
13 changes: 2 additions & 11 deletions src/mdo/writer-stax.vm
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ public class ${className} {
private String fileComment = null;

#if ( $locationTracking )
private boolean addLocationInformation = true;

/**
* Field stringFormatter.
*/
Expand Down Expand Up @@ -149,13 +147,6 @@ public class ${className} {
} //-- void setFileComment(String)

#if ( $locationTracking )
/**
* Method setAddLocationInformation.
*/
public void setAddLocationInformation(boolean addLocationInformation) {
this.addLocationInformation = addLocationInformation;
} //-- void setAddLocationInformation(String)

/**
* Method setStringFormatter.
*
Expand Down Expand Up @@ -383,7 +374,7 @@ public class ${className} {
}
serializer.writeEndElement();
#if ( $locationTracking )
if (addLocationInformation && dom.inputLocation() instanceof InputLocation inputLocation && dom.children().isEmpty()) {
if (stringFormatter != null && dom.inputLocation() instanceof InputLocation inputLocation && dom.children().isEmpty()) {
serializer.writeComment(toString(inputLocation));
}
#end
Expand Down Expand Up @@ -421,7 +412,7 @@ public class ${className} {
* @throws IOException
*/
protected void writeLocationTracking(InputLocationTracker locationTracker, Object key, XMLStreamWriter serializer) throws IOException, XMLStreamException {
if (addLocationInformation) {
if (stringFormatter != null) {
InputLocation location = (locationTracker == null) ? null : locationTracker.getLocation(key);
if (location != null) {
serializer.writeComment(toString(location));
Expand Down
Loading