Skip to content

Commit 5f9eb5f

Browse files
author
Guillaume Boué
committed
[MCHECKSTYLE-337] checkstyle:check only supports xml output format, but the docs say it supports plain as well
Submitted by: Stig Rohde Døssing Adding support for 'plain' output file format in the 'check' goal. It is still not possible to use it in combination with 'skipExec' because the plain format does not interact fully with 'violationIgnore' (which can contain fully qualified class name, when Checkstyle plain format only has the class simple name). git-svn-id: https://svn.apache.org/repos/asf/maven/plugins/trunk@1797596 13f79535-47bb-0310-9956-ffa450edef68
1 parent 7abd8b6 commit 5f9eb5f

File tree

9 files changed

+360
-8
lines changed

9 files changed

+360
-8
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
invoker.goals = verify
19+
invoker.buildResult = failure
20+
invoker.mavenOpts = -Duser.language=en -Duser.country=US -Duser.variant=US

src/it/MCHECKSTYLE-337/pom.xml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one
4+
~ or more contributor license agreements. See the NOTICE file
5+
~ distributed with this work for additional information
6+
~ regarding copyright ownership. The ASF licenses this file
7+
~ to you under the Apache License, Version 2.0 (the
8+
~ "License"); you may not use this file except in compliance
9+
~ with the License. You may obtain a copy of the License at
10+
~
11+
~ http://www.apache.org/licenses/LICENSE-2.0
12+
~
13+
~ Unless required by applicable law or agreed to in writing,
14+
~ software distributed under the License is distributed on an
15+
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
~ KIND, either express or implied. See the License for the
17+
~ specific language governing permissions and limitations
18+
~ under the License.
19+
-->
20+
21+
<project xmlns="http://maven.apache.org/POM/4.0.0"
22+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
24+
<modelVersion>4.0.0</modelVersion>
25+
26+
<groupId>org.apache.maven.plugins.checkstyle</groupId>
27+
<artifactId>MCHECKSTYLE-337</artifactId>
28+
<version>1.0-SNAPSHOT</version>
29+
<packaging>jar</packaging>
30+
31+
<url>http://maven.apache.org/</url>
32+
33+
<properties>
34+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
35+
<checkstyle.violation.ignore>NewlineAtEndOfFile</checkstyle.violation.ignore>
36+
</properties>
37+
38+
<build>
39+
<plugins>
40+
<plugin>
41+
<groupId>org.apache.maven.plugins</groupId>
42+
<artifactId>maven-checkstyle-plugin</artifactId>
43+
<version>@pom.version@</version>
44+
<executions>
45+
<execution>
46+
<id>check</id>
47+
<goals>
48+
<goal>check</goal>
49+
</goals>
50+
<configuration>
51+
<outputFile>${project.build.directory}/checkstyle-result.txt</outputFile>
52+
<outputFileFormat>plain</outputFileFormat>
53+
</configuration>
54+
</execution>
55+
</executions>
56+
</plugin>
57+
</plugins>
58+
</build>
59+
</project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org;
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
/**
23+
* My class.
24+
*/
25+
public class MyClass
26+
{
27+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Nothing very important here, only a file for checkstyle rule PackageInfo.
3+
*/
4+
package org;
5+
6+
/*
7+
* Licensed to the Apache Software Foundation (ASF) under one
8+
* or more contributor license agreements. See the NOTICE file
9+
* distributed with this work for additional information
10+
* regarding copyright ownership. The ASF licenses this file
11+
* to you under the Apache License, Version 2.0 (the
12+
* "License"); you may not use this file except in compliance
13+
* with the License. You may obtain a copy of the License at
14+
*
15+
* http://www.apache.org/licenses/LICENSE-2.0
16+
*
17+
* Unless required by applicable law or agreed to in writing,
18+
* software distributed under the License is distributed on an
19+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20+
* KIND, either express or implied. See the License for the
21+
* specific language governing permissions and limitations
22+
* under the License.
23+
*/

src/it/MCHECKSTYLE-337/verify.groovy

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
/*
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
*/
20+
def buildLog = new File( basedir, 'build.log' )
21+
22+
assert buildLog.text.contains( "You have 1 Checkstyle violation" )
23+
24+
def checkstyleResult = new File( basedir, "target/checkstyle-result.txt" )
25+
26+
assert checkstyleResult.text.contains( "'{' at column 1 should be on the previous line. [LeftCurly]" )

src/main/java/org/apache/maven/plugins/checkstyle/CheckstyleViolationCheckMojo.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,8 @@ public class CheckstyleViolationCheckMojo
485485

486486
private ByteArrayOutputStream stringOutputStream;
487487

488+
private File outputXmlFile;
489+
488490
/** {@inheritDoc} */
489491
public void execute()
490492
throws MojoExecutionException, MojoFailureException
@@ -496,6 +498,8 @@ public void execute()
496498
return;
497499
}
498500

501+
outputXmlFile = outputFile;
502+
499503
if ( !skipExec )
500504
{
501505
if ( checkstyleRules != null )
@@ -564,19 +568,19 @@ public void execute()
564568
}
565569
}
566570

567-
if ( !"xml".equals( outputFileFormat ) )
571+
if ( !"xml".equals( outputFileFormat ) && skipExec )
568572
{
569573
throw new MojoExecutionException( "Output format is '" + outputFileFormat
570-
+ "', checkstyle:check requires format to be 'xml'." );
574+
+ "', checkstyle:check requires format to be 'xml' when using skipExec." );
571575
}
572576

573-
if ( !outputFile.exists() )
577+
if ( !outputXmlFile.exists() )
574578
{
575579
getLog().info( "Unable to perform checkstyle:check, unable to find checkstyle:checkstyle outputFile." );
576580
return;
577581
}
578582

579-
try ( Reader reader = new BufferedReader( ReaderFactory.newXmlReader( outputFile ) ) )
583+
try ( Reader reader = new BufferedReader( ReaderFactory.newXmlReader( outputXmlFile ) ) )
580584
{
581585
XmlPullParser xpp = new MXParser();
582586
xpp.setInput( reader );
@@ -601,8 +605,8 @@ public void execute()
601605
}
602606
catch ( IOException | XmlPullParserException e )
603607
{
604-
throw new MojoExecutionException( "Unable to read Checkstyle results xml: " + outputFile.getAbsolutePath(),
605-
e );
608+
throw new MojoExecutionException( "Unable to read Checkstyle results xml: "
609+
+ outputXmlFile.getAbsolutePath(), e );
606610
}
607611
}
608612

@@ -799,7 +803,22 @@ private AuditListener getListener()
799803
}
800804
else if ( "plain".equals( outputFileFormat ) )
801805
{
802-
listener = new DefaultLogger( out, true );
806+
try
807+
{
808+
// Write a plain output file to the standard output file,
809+
// and write an XML output file to the temp directory that can be used to count violations
810+
outputXmlFile = File.createTempFile( "checkstyle-result", ".xml" );
811+
outputXmlFile.deleteOnExit();
812+
OutputStream xmlOut = getOutputStream( outputXmlFile );
813+
CompositeAuditListener compoundListener = new CompositeAuditListener();
814+
compoundListener.addListener( new XMLLogger( xmlOut, true ) );
815+
compoundListener.addListener( new DefaultLogger( out, true ) );
816+
listener = compoundListener;
817+
}
818+
catch ( IOException e )
819+
{
820+
throw new MojoExecutionException( "Unable to create temporary file", e );
821+
}
803822
}
804823
else
805824
{
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package org.apache.maven.plugins.checkstyle;
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
import java.util.ArrayList;
23+
import java.util.List;
24+
25+
import com.puppycrawl.tools.checkstyle.api.AuditEvent;
26+
import com.puppycrawl.tools.checkstyle.api.AuditListener;
27+
28+
/**
29+
* AuditListener that forwards events to a list of other AuditListeners
30+
*/
31+
public class CompositeAuditListener
32+
implements AuditListener
33+
{
34+
35+
private final List<AuditListener> delegates = new ArrayList<>();
36+
37+
public void addListener( AuditListener listener )
38+
{
39+
delegates.add( listener );
40+
}
41+
42+
@Override
43+
public void auditStarted( AuditEvent event )
44+
{
45+
for ( AuditListener listener : delegates )
46+
{
47+
listener.auditStarted( event );
48+
}
49+
}
50+
51+
@Override
52+
public void auditFinished( AuditEvent event )
53+
{
54+
for ( AuditListener listener : delegates )
55+
{
56+
listener.auditFinished( event );
57+
}
58+
}
59+
60+
@Override
61+
public void fileStarted( AuditEvent event )
62+
{
63+
for ( AuditListener listener : delegates )
64+
{
65+
listener.fileStarted( event );
66+
}
67+
}
68+
69+
@Override
70+
public void fileFinished( AuditEvent event )
71+
{
72+
for ( AuditListener listener : delegates )
73+
{
74+
listener.fileFinished( event );
75+
}
76+
}
77+
78+
@Override
79+
public void addError( AuditEvent event )
80+
{
81+
for ( AuditListener listener : delegates )
82+
{
83+
listener.addError( event );
84+
}
85+
}
86+
87+
@Override
88+
public void addException( AuditEvent event, Throwable throwable )
89+
{
90+
for ( AuditListener listener : delegates )
91+
{
92+
listener.addException( event, throwable );
93+
}
94+
}
95+
96+
}

src/test/java/org/apache/maven/plugins/checkstyle/CheckstyleViolationCheckMojoTest.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.apache.maven.plugin.Mojo;
2727
import org.apache.maven.plugin.MojoExecutionException;
2828
import org.apache.maven.plugin.MojoFailureException;
29+
import org.apache.maven.plugin.descriptor.PluginDescriptor;
2930
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
3031
import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
3132
import org.apache.maven.plugins.checkstyle.CheckstyleViolationCheckMojo;
@@ -65,7 +66,7 @@ public void testDefaultConfig()
6566
}
6667
}
6768

68-
public void testInvalidFormat()
69+
public void testInvalidFormatWithSkipExec()
6970
throws Exception
7071
{
7172
File pluginXmlFile = new File( getBasedir(), "src/test/plugin-configs/check-plugin-config.xml" );
@@ -106,6 +107,46 @@ public void testNoOutputFile()
106107
mojo.execute();
107108
}
108109

110+
private void doTestPlainOutputFile( boolean failsOnError )
111+
throws Exception
112+
{
113+
File pluginXmlFile = new File( getBasedir(), "src/test/plugin-configs/check-plugin-plain-output.xml" );
114+
115+
Mojo mojo = lookupMojo( "check", pluginXmlFile );
116+
117+
assertNotNull( "Mojo found.", mojo );
118+
119+
PluginDescriptor descriptorStub = new PluginDescriptor();
120+
descriptorStub.setGroupId( "org.apache.maven.plugins" );
121+
descriptorStub.setArtifactId( "maven-checkstyle-plugin" );
122+
setVariableValueToObject( mojo, "plugin", descriptorStub );
123+
124+
setVariableValueToObject( mojo, "failsOnError", failsOnError );
125+
126+
mojo.execute();
127+
}
128+
129+
public void testPlainOutputFileFailOnError()
130+
throws Exception
131+
{
132+
try
133+
{
134+
doTestPlainOutputFile( true );
135+
136+
fail( "Must fail on violations" );
137+
}
138+
catch ( MojoExecutionException e )
139+
{
140+
// expected
141+
}
142+
}
143+
144+
public void testPlainOutputFile()
145+
throws Exception
146+
{
147+
doTestPlainOutputFile( false );
148+
}
149+
109150
public void testNoFail()
110151
throws Exception
111152
{

0 commit comments

Comments
 (0)