Skip to content
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

XML formatter issue in maven plugin #404

Closed
parameshjava opened this issue May 31, 2019 · 11 comments
Closed

XML formatter issue in maven plugin #404

parameshjava opened this issue May 31, 2019 · 11 comments
Labels

Comments

@parameshjava
Copy link

parameshjava commented May 31, 2019

  • This issue is related maven plugin for formatting & validating XML file.
  • Below is the sample code that I am using in pom.xml file.
<plugin>
    <groupId>com.diffplug.spotless</groupId>
    <artifactId>spotless-maven-plugin</artifactId>
    <version>1.23.0</version>
    <configuration>
        <formats>
            <format>
                <encoding>Cp1252</encoding>
                <includes>
                    <include>pom.xml</include>
                </includes>
                <eclipseWtp>
                    <type>XML</type>
                    <files>
                        <file>${basedir}/DevStyleXml.prefs</file>
                    </files>
                    <version>4.7.3a</version>
                </eclipseWtp>
            </format>
        </formats>
        <encoding>US-ASCII</encoding>
    </configuration>
    <executions>
        <execution>
            <phase>compile</phase>
            <goals>
                <goal>check</goal>
            </goals>
        </execution>
    </executions>
</plugin>

DevStyleXml.prefs

eclipse.preferences.version=1
indentationChar=tab
indentationSize=1
lineWidth=999

Also tried with below.

eclipse.preferences.version=1
indentationChar=space
indentationSize=2
lineWidth=999
  • Also added .gitattributes with the following content.
* text eol=lf

In another trail, use the below content.

* text=auto
  • Spotless Version : 1.23.0
  • Issue : It's working fine in local systems (Windows, Mac, Unix) in developer's system. But failing in Jenkins. Below is the stacktrace in Jenkins.
20:56:45 [ERROR] Failed to execute goal com.diffplug.spotless:spotless-maven-plugin:1.23.0:check (default) on project ie-batch: The following files had format violations:
20:56:45 [ERROR] pom.xml
20:56:45 [ERROR] @@ -1,25 +1,24 @@
20:56:45 [ERROR] <?xml?version="1.0"?encoding="UTF-8"?>
20:56:45 [ERROR] -<project?xsi:schemaLocation="http://maven.apache.org/POM/4.0.0?http://maven.apache.org/xsd/maven-4.0.0.xsd"?xmlns="http://maven.apache.org/POM/4.0.0"
20:56:45 [ERROR] -????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
20:56:45 [ERROR] -??<modelVersion>4.0.0</modelVersion>
20:56:45 [ERROR] -??<parent>

I am not sure, whether I am missing anything here or it's a defect.

@nedtwigg nedtwigg added the bug label May 31, 2019
@fvgh
Copy link
Member

fvgh commented Jun 1, 2019

#369 solved a problem where WTP results can b different depending whether an XSD (maven-4.0.0.xsd) lookup fails or not.
This is the only reason I am aware of, why WTP results should be different on the same OS / same JVM.

@parameshjava
Is there a reason why you use still the old 4.7.3a? If you are not interested in a particular version, you can remove the version element.
I would appreciate if you could retry with a latter version containing the fix for #369, either by removing the version element or using for example 4.8.0 instead of 4.7.3a.

Have you tried using mvn spotless:apply on the OS where Jenkins is running, using the same JVM?

BTW: You tell Spotless to read/write the file as Cp1252 and claim in the XML header that it is UTF-8.

@parameshjava
Copy link
Author

@fvgh thanks for the quick reply.

  • In my pom.xml, used <?xml version="1.0" encoding="UTF-8"?> in the first line.
  • Removed version 4.7.3a, so it should uses the latest version.
  • As mentioned in the issue, I am using <encoding>Cp1252</encoding> inside <format> tag.

Please let me know, still I am missing anything.

In local only I can apply mvn spotless:apply, we configured read-only credentials in Jenkins server to fetch the code from git repo, it cannot commit back the code, so if I apply also there is no use of it.

@fvgh
Copy link
Member

fvgh commented Jun 3, 2019

@parameshjava
The encoding configuration was just a remark, since UTF-8 and Cp1252 are not fully compatible. But this should not have an impact on the problem you described.

So you say that the problem still occurs with the latest version of WTP, yes?

About the mvn spotless:apply: I did not want to you to commit the changed code, but to confirm that the spotless:apply leads on your Jenkins system to different results than on all your other systems (using the latest WTP version).

It would be nice if you could describe the differences. Looking at the error message, I would say that the indentation and/or line ending is different, but how? Is it tabs/versus spaces? Line-ending? Is the whole document affected or just parts? If just parts are different, which XML elements of the POM are affected?

@parameshjava
Copy link
Author

parameshjava commented Jun 3, 2019

@fvgh When I use mvn spotless:apply in Jenkins machine, it's getting success. I just downloaded pom.xml from jar file, compared with beyoned comparator, observed there is no difference in both the files.

@parameshjava
Copy link
Author

parameshjava commented Jun 3, 2019

Below is the error log, I can find in the jenkins.

22:49:36 [ERROR] Failed to execute goal com.diffplug.spotless:spotless-maven-plugin:1.23.0:check (default) on project batch-processing: The following files had format violations:
22:49:36 [ERROR] pom.xml
22:49:36 [ERROR] @@ -1,25 +1,24 @@
22:49:36 [ERROR] <?xml?version="1.0"?encoding="UTF-8"?>
22:49:36 [ERROR] -<project?xsi:schemaLocation="http://maven.apache.org/POM/4.0.0?http://maven.apache.org/xsd/maven-4.0.0.xsd"?xmlns="http://maven.apache.org/POM/4.0.0"
22:49:36 [ERROR] -????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
22:49:36 [ERROR] -??<modelVersion>4.0.0</modelVersion>
22:49:36 [ERROR] -??<parent>
22:49:36 [ERROR] -????<groupId>com.mycompany</groupId>
22:49:36 [ERROR] -????<artifactId>parent</artifactId>
22:49:36 [ERROR] -????<version>2.0.4</version>
22:49:36 [ERROR] -??</parent>
22:49:36 [ERROR] -??<groupId>com.mycompany.batch</groupId>
22:49:36 [ERROR] -??<artifactId>batch-processing</artifactId>
22:49:36 [ERROR] -??<version>0.0.1.7bb2c52</version>
22:49:36 [ERROR] -??<name>artifactId</name>
22:49:36 [ERROR] -??<description>Batch?Processing</description>
22:49:36 [ERROR] -??<properties>
22:49:36 [ERROR] -????<coverage.reports.dir>${basedir}/target/coverage-reports</coverage.reports.dir>
22:49:36 [ERROR] -????<hibernate.version>5.2.3.Final</hibernate.version>
22:49:36 [ERROR] -????<spring.batch.version>3.0.9.RELEASE</spring.batch.version>
22:49:36 [ERROR] -????<sonar.version>2.4</sonar.version>
22:49:36 [ERROR] -????<spring.boot.version>1.5.15.RELEASE</spring.boot.version>
22:49:36 [ERROR] +<project?xsi:schemaLocation="http://maven.apache.org/POM/4.0.0?http://maven.apache.org/xsd/maven-4.0.0.xsd"?xmlns="http://maven.apache.org/POM/4.0.0"?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
22:49:36 [ERROR] +\t<modelVersion>4.0.0</modelVersion>
22:49:36 [ERROR] +\t<parent>
22:49:36 [ERROR] +\t\t<groupId>com.mycompany</groupId>
22:49:36 [ERROR] +\t\t<artifactId>parent</artifactId>
22:49:36 [ERROR] +\t\t<version>2.0.4</version>
22:49:36 [ERROR] +\t</parent>
22:49:36 [ERROR] +\t<groupId>com.mycompany.batch</groupId>
22:49:36 [ERROR] +\t<artifactId>batch-processing</artifactId>
22:49:36 [ERROR] +\t<version>0.0.1.7bb2c52</version>
22:49:36 [ERROR] +\t<name>batch-processing</name>
22:49:36 [ERROR] +\t<description>Batch?Processing</description>
22:49:36 [ERROR] +\t<properties>
22:49:36 [ERROR] +\t\t<coverage.reports.dir>${basedir}/target/coverage-reports</coverage.reports.dir>
22:49:36 [ERROR] +\t\t<hibernate.version>5.2.3.Final</hibernate.version>
22:49:36 [ERROR] +\t\t<spring.batch.version>3.0.9.RELEASE</spring.batch.version>
22:49:36 [ERROR] +\t\t<sonar.version>2.4</sonar.version>
22:49:36 [ERROR] +\t\t<spring.boot.version>1.5.15.RELEASE</spring.boot.version>
22:49:36 [ERROR] +\t\t<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
22:49:36 [ERROR] ... (926 more lines that didn't fit)
22:49:36 [ERROR] Run 'mvn spotless:apply' to fix these violations.

@fvgh
Copy link
Member

fvgh commented Jun 3, 2019

To get this right:

  • The DevStyleXml.prefs in GIT repository (bare repository / origin) contains indentationChar=space, indentationSize=2
  • The DevStyleXml.prefs checkout on Jenkins contains indentationChar=space, indentationSize=2
  • The pom.xml in GIT repository using space indentation.
  • The pom.xml in GIT checkout on Jenkins using space indentation.
  • The pom.xml in GIT checkout on Jenkins still using space indentation after running mvn spotless:apply
  • When running mvn spotless:check on Jenkins, it complains that tabs shall be used

Correct so far?

@parameshjava
Copy link
Author

Yes correct.

@fvgh
Copy link
Member

fvgh commented Jun 3, 2019

Since spotless:check and spotless:apply are identical (using the same Spotless Java implementation) except that the first one compares output with input and the latter on just writes back the output to the input file, something in the build script must be weird. At least I have no other explanation.

I would recommend to check that ${basedir}/DevStyleXml.prefs really points to the file you are expecting and that the file contains what you are expecting.

Modify your pom.xml config for example to ${basedir}/DevStyleXml.dose.not.exist and let Jenkins run again. You should get an error message pointing to the path of <folder name>/DevStyleXml.dose.not.exist. Check that <folder name>/DevStyleXml.xml really contains indentationChar=space.

If this does not give you a hint where the problem might be, I would recommend to add an Ant task to your build script which copies DevStyleXml.xml and pom.xml to an absolute path (/tmp, C:\tmp, or something similar). Check the files manually after the Jenkins build. DevStyleXml.xml should contain indentationChar=space and pom.xml should use spaces for indentation.

@nedtwigg
Copy link
Member

nedtwigg commented Jun 3, 2019

I agree with @fvgh's assessment that there's something weird with DevStyleXml.prefs getting picked up by CI. Perhaps it's something to do with ambiguous behavior of basedir in a multi-module project?

@parameshjava
Copy link
Author

@fvgh @nedtwigg Thanks for your time. I after analysed my pipeline library, understood the issue. Pipeline library taking the existing pom.xml and modifying the version from SNAPSHOT to latest cut version. During this process, pom.xml modified by pipeline library is not aligned with the style given. Hence, this issue happened my CI pipeline.

To cross verify this, added some dummy xml with valid format and found working fine.

Sorry for faulty reported issue. Closing this issue.

@fvgh
Copy link
Member

fvgh commented Jun 4, 2019

@parameshjava
No worries. Always happy to hear that people using the Spotless Eclipse based formatters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants