├── .gitignore
├── src
├── etc
│ └── header.txt
├── test
│ ├── projects
│ │ ├── example-project
│ │ │ ├── module2
│ │ │ │ └── pom.xml
│ │ │ ├── module5
│ │ │ │ └── pom.xml
│ │ │ ├── module3
│ │ │ │ └── pom.xml
│ │ │ ├── module4
│ │ │ │ └── pom.xml
│ │ │ └── module1
│ │ │ │ └── pom.xml
│ │ ├── issue-2
│ │ │ ├── jar-module
│ │ │ │ └── pom.xml
│ │ │ ├── pom-module
│ │ │ │ └── pom.xml
│ │ │ └── pom.xml
│ │ ├── issue-53
│ │ │ ├── module1
│ │ │ │ └── pom.xml
│ │ │ └── pom.xml
│ │ ├── plugins
│ │ │ └── pom.xml
│ │ ├── warn-only
│ │ │ └── pom.xml
│ │ ├── simple-project
│ │ │ └── pom.xml
│ │ └── issue-23
│ │ │ └── pom.xml
│ └── java
│ │ └── com
│ │ └── github
│ │ └── ferstl
│ │ └── maven
│ │ └── pomenforcers
│ │ ├── model
│ │ ├── DependencyElementTest.java
│ │ ├── ModelTest.java
│ │ ├── CollectionToStringHelperTest.java
│ │ ├── DependencyModelTest.java
│ │ ├── functions
│ │ │ └── PluginMatcherTest.java
│ │ └── ArtifactModelTest.java
│ │ ├── ErrorReportAssert.java
│ │ ├── util
│ │ ├── CommaSeparatorUtilsTest.java
│ │ └── EnforcerRuleUtilsTest.java
│ │ ├── PedanticDependencyOrderEnforcerTest.java
│ │ ├── PedanticDependencyManagementOrderEnforcerTest.java
│ │ ├── priority
│ │ └── PriorityOrderingTest.java
│ │ ├── PedanticModuleOrderEnforcerTest.java
│ │ ├── PedanticPluginManagementLocationEnforcerTest.java
│ │ ├── PedanticDependencyElementEnforcerTest.java
│ │ ├── PedanticPluginElementEnforcerTest.java
│ │ ├── PedanticDependencyManagementLocationEnforcerTest.java
│ │ ├── PedanticPluginManagementOrderEnforcerTest.java
│ │ ├── PedanticPomEnforcersIntegrationTest.java
│ │ ├── PedanticPomSectionOrderEnforcerTest.java
│ │ ├── AbstractPedanticDependencyOrderEnforcerTest.java
│ │ ├── PedanticDependencyScopeEnforcerTest.java
│ │ └── AbstractPedanticEnforcerTest.java
└── main
│ └── java
│ └── com
│ └── github
│ └── ferstl
│ └── maven
│ └── pomenforcers
│ ├── priority
│ ├── PriorityOrderingFactory.java
│ ├── CompoundPriorityOrdering.java
│ └── PriorityOrdering.java
│ ├── model
│ ├── package-info.java
│ ├── DependencyScopeAdapter.java
│ ├── functions
│ │ ├── StringStartsWithEquivalence.java
│ │ ├── StringToArtifactTransformer.java
│ │ ├── PluginMatcher.java
│ │ ├── AbstractOneToOneMatcher.java
│ │ └── DependencyMatcher.java
│ ├── CollectionToStringHelper.java
│ ├── PluginManagementModel.java
│ ├── PluginsModel.java
│ ├── DependencyManagementModel.java
│ ├── DependencyScope.java
│ ├── DependenciesModel.java
│ ├── BuildModel.java
│ ├── PluginModel.java
│ ├── PomSection.java
│ ├── PluginElement.java
│ ├── DependencyElement.java
│ ├── DependencyModel.java
│ ├── ProjectModel.java
│ └── ArtifactModel.java
│ ├── util
│ ├── CommaSeparatorUtils.java
│ ├── EnforcerRuleUtils.java
│ └── XmlUtils.java
│ ├── PedanticEnforcerVisitor.java
│ ├── PedanticDependencyOrderEnforcer.java
│ ├── AbstractPedanticEnforcer.java
│ ├── PedanticDependencyManagementOrderEnforcer.java
│ ├── ErrorReport.java
│ ├── PedanticModuleOrderEnforcer.java
│ ├── PedanticPomSectionOrderEnforcer.java
│ ├── PedanticPluginManagementLocationEnforcer.java
│ ├── PedanticDependencyManagementLocationEnforcer.java
│ ├── AbstractPedanticDependencyOrderEnforcer.java
│ └── PedanticEnforcerRule.java
└── .github
└── workflows
├── maven.yml
└── codeql-analysis.yml
/.gitignore:
--------------------------------------------------------------------------------
1 | .idea/
2 | target/
3 | *.iml
4 | .flattened-pom.xml
5 |
--------------------------------------------------------------------------------
/src/etc/header.txt:
--------------------------------------------------------------------------------
1 | Copyright (c) 2012 - 2025 the original author or authors.
2 |
3 | Licensed under the Apache License, Version 2.0 (the "License");
4 | you may not use this file except in compliance with the License.
5 | You may obtain a copy of the License at
6 |
7 | http://www.apache.org/licenses/LICENSE-2.0
8 |
9 | Unless required by applicable law or agreed to in writing, software
10 | distributed under the License is distributed on an "AS IS" BASIS,
11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | See the License for the specific language governing permissions and
13 | limitations under the License.
14 |
--------------------------------------------------------------------------------
/src/test/projects/example-project/module2/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 |
6 | com.github.ferstl
7 | example-settings-it
8 | 1.0-SNAPSHOT
9 |
10 |
11 | module2
12 |
13 |
14 |
15 | ${project.groupId}
16 | module1
17 | ${project.version}
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/src/test/projects/example-project/module5/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 |
6 | com.github.ferstl
7 | example-settings-it
8 | 1.0-SNAPSHOT
9 |
10 |
11 | module5
12 |
13 |
14 |
15 | ${project.groupId}
16 | module1
17 | ${project.version}
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/src/test/projects/issue-2/jar-module/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 |
6 | com.github.ferstl
7 | issue-2-it-parent
8 | 1.0-SNAPSHOT
9 |
10 |
11 | issue-2-it-jar
12 |
13 |
14 |
15 |
16 | ${project.groupId}
17 | issue-2-it-pom
18 | ${project.version}
19 | pom
20 | import
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/src/test/projects/issue-53/module1/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 |
6 | com.github.ferstl
7 | issue-53-parent
8 | 1.0-SNAPSHOT
9 |
10 |
11 | module1
12 |
13 |
14 |
15 |
16 | org.springframework
17 | spring-jdbc
18 |
19 |
20 | *
21 | *
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ferstl/maven/pomenforcers/priority/PriorityOrderingFactory.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 - 2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.ferstl.maven.pomenforcers.priority;
17 |
18 | import java.util.Collection;
19 |
20 |
21 | public interface PriorityOrderingFactory
, T> {
22 |
23 | PriorityOrdering
createPriorityOrdering(Collection
priorityCollection);
24 | }
25 |
--------------------------------------------------------------------------------
/src/test/projects/issue-2/pom-module/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 |
6 | com.github.ferstl
7 | issue-2-it-parent
8 | 1.0-SNAPSHOT
9 |
10 |
11 | issue-2-it-pom
12 | pom
13 |
14 |
15 |
16 |
17 | com.googlecode.lambdaj
18 | lambdaj
19 | 2.3.3
20 |
21 |
22 |
23 |
24 |
25 |
26 | com.googlecode.lambdaj
27 | lambdaj
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/src/test/projects/example-project/module3/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 |
6 | com.github.ferstl
7 | example-settings-it
8 | 1.0-SNAPSHOT
9 |
10 |
11 | module3
12 |
13 |
14 |
15 | ${project.groupId}
16 | module1
17 | ${project.version}
18 |
19 |
20 |
21 | ${project.groupId}
22 | module1
23 | ${project.version}
24 | tests
25 | test
26 |
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ferstl/maven/pomenforcers/model/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 - 2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | @XmlSchema(
17 | namespace = "http://maven.apache.org/POM/4.0.0",
18 | location = "http://maven.apache.org/xsd/maven-4.0.0.xsd")
19 | @XmlAccessorType(FIELD)
20 | package com.github.ferstl.maven.pomenforcers.model;
21 |
22 | import javax.xml.bind.annotation.XmlAccessorType;
23 | import javax.xml.bind.annotation.XmlSchema;
24 | import static javax.xml.bind.annotation.XmlAccessType.FIELD;
25 |
26 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ferstl/maven/pomenforcers/model/DependencyScopeAdapter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 - 2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.ferstl.maven.pomenforcers.model;
17 |
18 | import javax.xml.bind.annotation.adapters.XmlAdapter;
19 |
20 |
21 | class DependencyScopeAdapter extends XmlAdapter {
22 |
23 | @Override
24 | public DependencyScope unmarshal(String v) {
25 | return DependencyScope.getByScopeName(v);
26 | }
27 |
28 | @Override
29 | public String marshal(DependencyScope v) {
30 | return v.getScopeName();
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/src/test/projects/plugins/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 | plugins
6 |
7 |
8 |
9 |
10 |
11 | org.apache.maven.plugins
12 | maven-jar-plugin
13 | 2.4
14 | false
15 | true
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 | org.apache.maven.plugins
25 | maven-jar-plugin
26 | 2.4
27 | false
28 | true
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/src/test/java/com/github/ferstl/maven/pomenforcers/model/DependencyElementTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 - 2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.ferstl.maven.pomenforcers.model;
17 |
18 | import org.junit.jupiter.api.Test;
19 | import static org.assertj.core.api.Assertions.assertThat;
20 |
21 |
22 | class DependencyElementTest {
23 |
24 | @Test
25 | void testGetByElementName() {
26 | DependencyElement.values();
27 | for (DependencyElement element : DependencyElement.values()) {
28 | assertThat(element).isEqualTo(DependencyElement.getByElementName(element.getElementName()));
29 | }
30 | }
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/.github/workflows/maven.yml:
--------------------------------------------------------------------------------
1 | name: Java CI
2 |
3 | on:
4 | push:
5 | branches: [ master ]
6 | pull_request:
7 | branches: [ master ]
8 | workflow_dispatch:
9 |
10 | jobs:
11 | build:
12 | runs-on: ubuntu-latest
13 | strategy:
14 | matrix:
15 | java: [ '11', '17', '21' ]
16 | steps:
17 | - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
18 | - name: Set up JDK ${{ matrix.Java }}
19 | uses: actions/setup-java@3a4f6e1af504cf6a31855fa899c6aa5355ba6c12 # v4.7.0
20 | with:
21 | java-version: ${{ matrix.Java }}
22 | distribution: 'zulu'
23 | cache: 'maven'
24 | - name: Maven Build
25 | run: mvn clean install javadoc:javadoc
26 |
27 | post-build:
28 | needs: [ build ]
29 | runs-on: ubuntu-latest
30 | steps:
31 | - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
32 | - name: Set up JDK
33 | uses: actions/setup-java@3a4f6e1af504cf6a31855fa899c6aa5355ba6c12 # v4.7.0
34 | with:
35 | java-version: 17
36 | distribution: 'zulu'
37 | cache: maven
38 | - name: Coveralls Report
39 | run: mvn org.jacoco:jacoco-maven-plugin:prepare-agent test org.jacoco:jacoco-maven-plugin:report org.eluder.coveralls:coveralls-maven-plugin:report -DrepoToken=${{ secrets.COVERALLS_TOKEN }}
40 |
--------------------------------------------------------------------------------
/src/test/projects/example-project/module4/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 |
6 | com.github.ferstl
7 | example-settings-it
8 | 1.0-SNAPSHOT
9 |
10 |
11 | module4
12 |
13 |
14 |
15 | ${project.groupId}
16 | module1
17 | ${project.version}
18 |
19 |
20 | ${project.groupId}
21 | module2
22 | ${project.version}
23 |
24 |
25 | ${project.groupId}
26 | module3
27 | ${project.version}
28 |
29 |
30 |
31 | ${project.groupId}
32 | module1
33 | ${project.version}
34 | test-jar
35 | test
36 |
37 |
38 |
39 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ferstl/maven/pomenforcers/model/functions/StringStartsWithEquivalence.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 - 2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.ferstl.maven.pomenforcers.model.functions;
17 |
18 | import com.google.common.base.Equivalence;
19 |
20 |
21 | public class StringStartsWithEquivalence extends Equivalence {
22 |
23 | private static final Equivalence INSTANCE = new StringStartsWithEquivalence();
24 |
25 | public static Equivalence stringStartsWith() {
26 | return INSTANCE;
27 | }
28 |
29 | @Override
30 | protected boolean doEquivalent(String a, String b) {
31 | return a.startsWith(b);
32 | }
33 |
34 | @Override
35 | protected int doHash(String t) {
36 | return t.hashCode();
37 | }
38 |
39 | private StringStartsWithEquivalence() {
40 | }
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ferstl/maven/pomenforcers/model/CollectionToStringHelper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 - 2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.ferstl.maven.pomenforcers.model;
17 |
18 | import com.google.common.base.Joiner;
19 | import com.google.common.collect.Iterables;
20 | import static java.util.Collections.emptyList;
21 |
22 | final class CollectionToStringHelper {
23 |
24 | private static final Joiner JOINER = Joiner.on(",\n");
25 |
26 | public static String toString(String prefix, Iterable> iterable) {
27 | Iterable> theIterable = iterable != null ? iterable : emptyList();
28 |
29 | StringBuilder sb = new StringBuilder(prefix).append(" [\n");
30 | JOINER.appendTo(sb, theIterable).append(!Iterables.isEmpty(theIterable) ? "\n]" : "]");
31 | return sb.toString();
32 | }
33 |
34 | private CollectionToStringHelper() {
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ferstl/maven/pomenforcers/model/functions/StringToArtifactTransformer.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 - 2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.ferstl.maven.pomenforcers.model.functions;
17 |
18 | import java.util.ArrayList;
19 | import com.github.ferstl.maven.pomenforcers.model.ArtifactModel;
20 | import com.google.common.base.Splitter;
21 | import com.google.common.collect.Lists;
22 |
23 | public final class StringToArtifactTransformer {
24 |
25 | private static final Splitter COLON_SPLITTER = Splitter.on(":");
26 |
27 | public static ArtifactModel toArtifactModel(String input) {
28 | ArrayList artifactElements = Lists.newArrayList(COLON_SPLITTER.split(input));
29 |
30 | if (artifactElements.size() != 2) {
31 | throw new IllegalArgumentException("Cannot read POM information: " + input);
32 | }
33 |
34 | return new ArtifactModel(artifactElements.get(0), artifactElements.get(1));
35 | }
36 |
37 | private StringToArtifactTransformer() {
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/test/java/com/github/ferstl/maven/pomenforcers/model/ModelTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 - 2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.ferstl.maven.pomenforcers.model;
17 |
18 | import java.io.File;
19 | import javax.xml.bind.Binder;
20 | import javax.xml.bind.JAXBContext;
21 | import javax.xml.bind.JAXBElement;
22 | import org.junit.jupiter.api.Test;
23 | import org.w3c.dom.Document;
24 | import org.w3c.dom.Node;
25 | import com.github.ferstl.maven.pomenforcers.util.XmlUtils;
26 | import static org.assertj.core.api.Assertions.assertThat;
27 |
28 | class ModelTest {
29 |
30 | @Test
31 | void test() throws Exception {
32 | Document pom = XmlUtils.parseXml(new File("src/test/projects/example-project/pom.xml"));
33 | JAXBContext ctx = JAXBContext.newInstance(ProjectModel.class);
34 |
35 | Binder binder = ctx.createBinder();
36 | JAXBElement projectModel = binder.unmarshal(pom, ProjectModel.class);
37 |
38 | assertThat(projectModel).isNotNull();
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/src/test/java/com/github/ferstl/maven/pomenforcers/model/CollectionToStringHelperTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 - 2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.ferstl.maven.pomenforcers.model;
17 |
18 | import org.junit.jupiter.api.Test;
19 | import static java.util.Arrays.asList;
20 | import static org.junit.jupiter.api.Assertions.assertEquals;
21 |
22 | class CollectionToStringHelperTest {
23 |
24 | @Test
25 | void toStringWithValues() {
26 | // act
27 | String result = CollectionToStringHelper.toString("Test", asList("a", "b", "c"));
28 |
29 | // assert
30 | String expected = "Test [\n"
31 | + "a,\n"
32 | + "b,\n"
33 | + "c\n"
34 | + "]";
35 | assertEquals(expected, result);
36 | }
37 |
38 |
39 | @Test
40 | void toStringWithNullCollection() {
41 | // act
42 | String result = CollectionToStringHelper.toString("Test", null);
43 |
44 | // assert
45 | String expected = "Test [\n"
46 | + "]";
47 | assertEquals(expected, result);
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/test/java/com/github/ferstl/maven/pomenforcers/ErrorReportAssert.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 - 2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.ferstl.maven.pomenforcers;
17 |
18 | import org.assertj.core.api.AbstractAssert;
19 |
20 | /**
21 | * Matcher that shows the {@link ErrorReport} in case of an unexpected failure.
22 | */
23 | class ErrorReportAssert extends AbstractAssert {
24 |
25 | public ErrorReportAssert(ErrorReport actual) {
26 | super(actual, ErrorReportAssert.class);
27 | }
28 |
29 | public static ErrorReportAssert assertThat(ErrorReport actual) {
30 | return new ErrorReportAssert(actual);
31 | }
32 |
33 | public ErrorReportAssert hasErrors() {
34 | if (!this.actual.hasErrors()) {
35 | failWithMessage("There were no errors");
36 | }
37 |
38 | return this;
39 | }
40 |
41 | public ErrorReportAssert hasNoErrors() {
42 | if (this.actual.hasErrors()) {
43 | failWithMessage("There were errors");
44 | }
45 |
46 | return this;
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/test/java/com/github/ferstl/maven/pomenforcers/model/DependencyModelTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 - 2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.ferstl.maven.pomenforcers.model;
17 |
18 | import org.junit.jupiter.api.Test;
19 | import static org.junit.jupiter.api.Assertions.assertEquals;
20 |
21 |
22 | class DependencyModelTest {
23 |
24 | @Test
25 | void toStringWithDefaults() {
26 | DependencyModel model = new DependencyModel("group", "artifact", "1.0.0", null, null, null);
27 |
28 | assertEquals("group:artifact:1.0.0:jar:compile", model.toString());
29 | }
30 |
31 | @Test
32 | void toStringWithClassifier() {
33 | DependencyModel model = new DependencyModel("group", "artifact", "1.0.0", null, "classifier", null);
34 |
35 | assertEquals("group:artifact:1.0.0:jar:compile:classifier", model.toString());
36 | }
37 |
38 | @Test
39 | void toStringNoDefaults() {
40 | DependencyModel model = new DependencyModel("group", "artifact", "1.0.0", "test", "classifier", "zip");
41 |
42 | assertEquals("group:artifact:1.0.0:zip:test:classifier", model.toString());
43 | }
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/src/test/java/com/github/ferstl/maven/pomenforcers/util/CommaSeparatorUtilsTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 - 2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.ferstl.maven.pomenforcers.util;
17 |
18 | import java.util.ArrayList;
19 | import java.util.List;
20 | import java.util.Map;
21 | import org.junit.jupiter.api.Test;
22 | import com.google.common.collect.ImmutableList;
23 | import com.google.common.collect.ImmutableMap;
24 | import static org.assertj.core.api.Assertions.assertThat;
25 |
26 | class CommaSeparatorUtilsTest {
27 |
28 | @Test
29 | void testSplitAndAddToCollection() {
30 | Map> tests = ImmutableMap.>builder()
31 | .put("a,b,c", ImmutableList.of("a", "b", "c"))
32 | .put("a,b,,,c,", ImmutableList.of("a", "b", "c"))
33 | .put(" a \n,\n \t b ,\r\n c \t\n", ImmutableList.of("a", "b", "c"))
34 | .build();
35 | for (Map.Entry> test : tests.entrySet()) {
36 | List l = new ArrayList<>();
37 | CommaSeparatorUtils.splitAndAddToCollection(test.getKey(), l);
38 | assertThat(l).isEqualTo(test.getValue());
39 | }
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ferstl/maven/pomenforcers/model/PluginManagementModel.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 - 2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.ferstl.maven.pomenforcers.model;
17 |
18 | import java.util.Collections;
19 | import java.util.List;
20 | import java.util.Objects;
21 | import javax.xml.bind.annotation.XmlElement;
22 |
23 | class PluginManagementModel {
24 |
25 | @XmlElement(namespace = "http://maven.apache.org/POM/4.0.0")
26 | private PluginsModel plugins;
27 |
28 | public List getPlugins() {
29 | return this.plugins != null ? this.plugins.getPlugins() : Collections.emptyList();
30 | }
31 |
32 | @Override
33 | public String toString() {
34 | return "PluginManagement->" + Objects.toString(this.plugins, "none");
35 | }
36 |
37 | @Override
38 | public boolean equals(Object obj) {
39 | if (obj == this) {
40 | return true;
41 | }
42 | if (!(obj instanceof PluginManagementModel)) {
43 | return false;
44 | }
45 | PluginManagementModel other = (PluginManagementModel) obj;
46 | return Objects.equals(this.plugins, other.plugins);
47 | }
48 |
49 | @Override
50 | public int hashCode() {
51 | return Objects.hash(this.plugins);
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ferstl/maven/pomenforcers/model/PluginsModel.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 - 2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.ferstl.maven.pomenforcers.model;
17 |
18 | import java.util.Collections;
19 | import java.util.List;
20 | import java.util.Objects;
21 | import javax.xml.bind.annotation.XmlElement;
22 |
23 | class PluginsModel {
24 |
25 | @XmlElement(name = "plugin", namespace = "http://maven.apache.org/POM/4.0.0")
26 | private List plugins;
27 |
28 | public PluginsModel() {
29 | }
30 |
31 | public List getPlugins() {
32 | return this.plugins != null ? this.plugins : Collections.emptyList();
33 | }
34 |
35 | @Override
36 | public String toString() {
37 | return CollectionToStringHelper.toString("Plugins", this.plugins);
38 | }
39 |
40 | @Override
41 | public boolean equals(Object obj) {
42 | if (obj == this) {
43 | return true;
44 | }
45 | if (!(obj instanceof PluginsModel)) {
46 | return false;
47 | }
48 | PluginsModel other = (PluginsModel) obj;
49 | return getPlugins().equals(other.getPlugins());
50 | }
51 |
52 | @Override
53 | public int hashCode() {
54 | return Objects.hash(getPlugins());
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ferstl/maven/pomenforcers/util/CommaSeparatorUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 - 2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.ferstl.maven.pomenforcers.util;
17 |
18 | import java.util.Collection;
19 | import java.util.function.Function;
20 | import java.util.stream.StreamSupport;
21 | import com.google.common.base.Splitter;
22 | import static java.util.stream.Collectors.toCollection;
23 |
24 | public final class CommaSeparatorUtils {
25 |
26 | private static final Splitter COMMA_SPLITTER = Splitter.on(",").trimResults().omitEmptyStrings();
27 |
28 | public static void splitAndAddToCollection(String commaSeparatedItems, Collection collection) {
29 | splitAndAddToCollection(commaSeparatedItems, collection, Function.identity());
30 | }
31 |
32 | public static void splitAndAddToCollection(String commaSeparatedItems, Collection collection, Function transformer) {
33 | Iterable items = COMMA_SPLITTER.split(commaSeparatedItems);
34 | // Don't touch the collection if there is nothing to add.
35 | if (items.iterator().hasNext()) {
36 | collection.clear();
37 | }
38 | StreamSupport.stream(items.spliterator(), false)
39 | .map(transformer)
40 | .collect(toCollection(() -> collection));
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/src/test/java/com/github/ferstl/maven/pomenforcers/PedanticDependencyOrderEnforcerTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 - 2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.ferstl.maven.pomenforcers;
17 |
18 | import org.junit.jupiter.api.Test;
19 | import static org.assertj.core.api.Assertions.assertThat;
20 | import static org.mockito.Mockito.mock;
21 | import static org.mockito.Mockito.verify;
22 |
23 | /**
24 | * JUnit tests for {@link PedanticDependencyOrderEnforcer}.
25 | */
26 | class PedanticDependencyOrderEnforcerTest extends AbstractPedanticDependencyOrderEnforcerTest {
27 |
28 | @Override
29 | PedanticDependencyOrderEnforcer createRule() {
30 | return new PedanticDependencyOrderEnforcer(this.mockMavenProject, this.mockHelper);
31 | }
32 |
33 | @Override
34 | @Test
35 | void getDescription() {
36 | assertThat(this.testRule.getDescription()).isEqualTo(PedanticEnforcerRule.DEPENDENCY_ORDER);
37 | }
38 |
39 | @Override
40 | @Test
41 | void accept() {
42 | PedanticEnforcerVisitor visitor = mock(PedanticEnforcerVisitor.class);
43 | this.testRule.accept(visitor);
44 |
45 | verify(visitor).visit(this.testRule);
46 | }
47 |
48 | @Override
49 | protected DependencyAdder createDependencyAdder() {
50 | return this::addDependency;
51 | }
52 |
53 | }
54 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ferstl/maven/pomenforcers/model/DependencyManagementModel.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 - 2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.ferstl.maven.pomenforcers.model;
17 |
18 | import java.util.Collections;
19 | import java.util.List;
20 | import java.util.Objects;
21 | import javax.xml.bind.annotation.XmlElement;
22 |
23 |
24 | class DependencyManagementModel {
25 |
26 | @XmlElement(namespace = "http://maven.apache.org/POM/4.0.0")
27 | private DependenciesModel dependencies;
28 |
29 | public List getDependencies() {
30 | return this.dependencies != null ? this.dependencies.getDependencies() : Collections.emptyList();
31 | }
32 |
33 | @Override
34 | public String toString() {
35 | return "DependencyManagement->" + Objects.toString(this.dependencies, "none");
36 | }
37 |
38 | @Override
39 | public boolean equals(Object obj) {
40 | if (obj == this) {
41 | return true;
42 | }
43 | if (!(obj instanceof DependencyManagementModel)) {
44 | return false;
45 | }
46 | DependencyManagementModel other = (DependencyManagementModel) obj;
47 | return Objects.equals(this.dependencies, other.dependencies);
48 | }
49 |
50 | @Override
51 | public int hashCode() {
52 | return this.dependencies != null ? this.dependencies.hashCode() : 0;
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ferstl/maven/pomenforcers/model/DependencyScope.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 - 2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.ferstl.maven.pomenforcers.model;
17 |
18 | import java.util.LinkedHashMap;
19 | import java.util.Map;
20 | import static java.util.Objects.requireNonNull;
21 |
22 | public enum DependencyScope {
23 |
24 | IMPORT("import"),
25 | COMPILE("compile"),
26 | PROVIDED("provided"),
27 | RUNTIME("runtime"),
28 | SYSTEM("system"),
29 | TEST("test");
30 |
31 | private static final Map dependencyScopeMap;
32 |
33 | static {
34 | dependencyScopeMap = new LinkedHashMap<>();
35 | for (DependencyScope scope : values()) {
36 | dependencyScopeMap.put(scope.getScopeName(), scope);
37 | }
38 | }
39 |
40 | public static DependencyScope getByScopeName(String scopeName) {
41 | requireNonNull(scopeName, "Scope name is null.");
42 |
43 | DependencyScope scope = dependencyScopeMap.get(scopeName);
44 | if (scope == null) {
45 | throw new IllegalArgumentException("Dependency scope'" + scopeName + "' does not exist.");
46 | }
47 |
48 | return scope;
49 | }
50 |
51 | private final String scopeName;
52 |
53 | DependencyScope(String name) {
54 | this.scopeName = name;
55 | }
56 |
57 | public String getScopeName() {
58 | return this.scopeName;
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/src/test/java/com/github/ferstl/maven/pomenforcers/PedanticDependencyManagementOrderEnforcerTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 - 2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.ferstl.maven.pomenforcers;
17 |
18 | import org.junit.jupiter.api.Test;
19 | import static org.assertj.core.api.Assertions.assertThat;
20 | import static org.mockito.Mockito.mock;
21 | import static org.mockito.Mockito.verify;
22 |
23 | /**
24 | * JUnit tests for {@link PedanticDependencyManagementOrderEnforcer}:
25 | */
26 | class PedanticDependencyManagementOrderEnforcerTest extends AbstractPedanticDependencyOrderEnforcerTest {
27 |
28 | @Override
29 | PedanticDependencyManagementOrderEnforcer createRule() {
30 | return new PedanticDependencyManagementOrderEnforcer(this.mockMavenProject, this.mockHelper);
31 | }
32 |
33 | @Test
34 | @Override
35 | void getDescription() {
36 | assertThat(this.testRule.getDescription()).isEqualTo(PedanticEnforcerRule.DEPENDENCY_MANAGEMENT_ORDER);
37 | }
38 |
39 | @Test
40 | @Override
41 | void accept() {
42 | PedanticEnforcerVisitor visitor = mock(PedanticEnforcerVisitor.class);
43 | this.testRule.accept(visitor);
44 |
45 | verify(visitor).visit(this.testRule);
46 | }
47 |
48 | @Override
49 | protected DependencyAdder createDependencyAdder() {
50 | return this::addManagedDependency;
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ferstl/maven/pomenforcers/PedanticEnforcerVisitor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 - 2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.ferstl.maven.pomenforcers;
17 |
18 | public interface PedanticEnforcerVisitor {
19 |
20 | void visit(PedanticPomSectionOrderEnforcer sectionOrderEnforcer);
21 |
22 | void visit(PedanticModuleOrderEnforcer moduleOrderEnforcer);
23 |
24 | void visit(PedanticDependencyManagementOrderEnforcer dependencyManagementOrderEnforcer);
25 |
26 | void visit(PedanticDependencyManagementLocationEnforcer pedanticDependencyManagementLocationEnforcer);
27 |
28 | void visit(PedanticDependencyOrderEnforcer dependencyOrderEnforcer);
29 |
30 | void visit(PedanticDependencyConfigurationEnforcer pedanticDependencyConfigurationEnforcer);
31 |
32 | void visit(PedanticDependencyScopeEnforcer pedanticDependencyScopeEnforcer);
33 |
34 | void visit(PedanticPluginManagementOrderEnforcer pluginManagementOrderEnforcer);
35 |
36 | void visit(CompoundPedanticEnforcer compoundEnforcer);
37 |
38 | void visit(PedanticPluginConfigurationEnforcer pedanticPluginConfigurationEnforcer);
39 |
40 | void visit(PedanticPluginManagementLocationEnforcer pedanticPluginManagementLocationEnforcer);
41 |
42 | void visit(PedanticDependencyElementEnforcer pedanticDependencyElementEnforcer);
43 |
44 | void visit(PedanticPluginElementEnforcer pedanticPluginElementEnforcer);
45 | }
46 |
--------------------------------------------------------------------------------
/src/test/java/com/github/ferstl/maven/pomenforcers/util/EnforcerRuleUtilsTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 - 2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.ferstl.maven.pomenforcers.util;
17 |
18 | import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
19 | import org.junit.jupiter.api.BeforeEach;
20 | import org.junit.jupiter.api.Test;
21 | import static com.github.ferstl.maven.pomenforcers.util.EnforcerRuleUtils.evaluateProperties;
22 | import static org.assertj.core.api.Assertions.assertThat;
23 | import static org.mockito.ArgumentMatchers.anyString;
24 | import static org.mockito.Mockito.mock;
25 | import static org.mockito.Mockito.when;
26 |
27 | class EnforcerRuleUtilsTest {
28 |
29 | private ExpressionEvaluator mockHelper;
30 |
31 | @BeforeEach
32 | void setup() throws Exception {
33 | this.mockHelper = mock(ExpressionEvaluator.class);
34 | when(this.mockHelper.evaluate(anyString())).thenReturn("test");
35 | }
36 |
37 | @Test
38 | void testEvaluateProperties() {
39 | assertThat(evaluateProperties("foo-${user.name}-bar", this.mockHelper)).isEqualTo("foo-test-bar");
40 | assertThat(evaluateProperties("foo-${x}-bar-${y}", this.mockHelper)).isEqualTo("foo-test-bar-test");
41 | assertThat(evaluateProperties("foo", this.mockHelper)).isEqualTo("foo");
42 | assertThat(evaluateProperties("", this.mockHelper)).isEqualTo("");
43 | assertThat(evaluateProperties(null, this.mockHelper)).isNull();
44 | }
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/src/test/projects/issue-2/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 | com.github.ferstl
6 | issue-2-it-parent
7 | 1.0-SNAPSHOT
8 | pom
9 |
10 |
11 | Integration test for issue #2. See https://github.com/ferstl/pedantic-pom-enforcers/pull/2 .
12 | This project contains two modules. pom-module has <packaging>pom</packaging>.
13 | jar-module has <packaging>jar</packaging> and an import-scope dependency to pom-module.
14 |
15 |
16 |
17 | jar-module
18 | pom-module
19 |
20 |
21 |
22 |
23 |
24 |
25 | org.apache.maven.plugins
26 | maven-enforcer-plugin
27 | 3.6.0
28 |
29 |
30 |
31 |
32 | DEPENDENCY_MANAGEMENT_ORDER,DEPENDENCY_ORDER
33 |
34 |
35 | true
36 |
37 |
38 |
39 | com.github.ferstl
40 | pedantic-pom-enforcers
41 | ${it-plugin.version}
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 | org.apache.maven.plugins
50 | maven-enforcer-plugin
51 |
52 |
53 |
54 |
55 |
--------------------------------------------------------------------------------
/src/test/projects/issue-53/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 | com.github.ferstl
6 | issue-53-parent
7 | 1.0-SNAPSHOT
8 | pom
9 |
10 |
11 | Investigatin of https://github.com/ferstl/pedantic-pom-enforcers/issues/53
12 |
13 |
14 |
15 | module1
16 |
17 |
18 |
19 |
20 |
21 | org.springframework
22 | spring-jdbc
23 | 6.2.12
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 | org.apache.maven.plugins
33 | maven-enforcer-plugin
34 | 3.6.0
35 |
36 |
37 |
38 |
39 |
40 | DEPENDENCY_CONFIGURATION
41 |
42 |
43 |
44 | true
45 |
46 |
47 |
48 | com.github.ferstl
49 | pedantic-pom-enforcers
50 | ${it-plugin.version}
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 | org.apache.maven.plugins
59 | maven-enforcer-plugin
60 |
61 |
62 |
63 |
64 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ferstl/maven/pomenforcers/model/DependenciesModel.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 - 2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.ferstl.maven.pomenforcers.model;
17 |
18 | import java.util.ArrayList;
19 | import java.util.Collection;
20 | import java.util.Collections;
21 | import java.util.List;
22 | import java.util.Objects;
23 | import javax.xml.bind.annotation.XmlElement;
24 |
25 | class DependenciesModel {
26 |
27 | @XmlElement(name = "dependency", namespace = "http://maven.apache.org/POM/4.0.0")
28 | private List dependencies;
29 |
30 | // Constructor used by JAXB
31 | DependenciesModel() {
32 | }
33 |
34 | public DependenciesModel(Collection dependencies) {
35 | this.dependencies = new ArrayList<>();
36 | this.dependencies.addAll(dependencies);
37 | }
38 |
39 |
40 | public List getDependencies() {
41 | return this.dependencies != null ? this.dependencies : Collections.emptyList();
42 | }
43 |
44 | @Override
45 | public boolean equals(Object obj) {
46 | if (obj == this) {
47 | return true;
48 | }
49 | if (!(obj instanceof DependenciesModel)) {
50 | return false;
51 | }
52 |
53 | DependenciesModel other = (DependenciesModel) obj;
54 | return getDependencies().equals(other.getDependencies());
55 | }
56 |
57 | @Override
58 | public int hashCode() {
59 | return Objects.hash(getDependencies());
60 | }
61 |
62 | @Override
63 | public String toString() {
64 | return CollectionToStringHelper.toString("Dependencies", this.dependencies);
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/src/test/projects/example-project/module1/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 |
6 | com.github.ferstl
7 | example-settings-it
8 | 1.0-SNAPSHOT
9 |
10 |
11 | module1
12 |
13 |
14 | commons-,org.hamcrest
15 |
16 |
17 |
18 |
19 | commons-lang
20 | commons-lang
21 |
22 |
23 | com.googlecode.lambdaj
24 | lambdaj
25 |
26 |
27 | javax.servlet
28 | servlet-api
29 |
30 |
31 | junit
32 | junit
33 |
34 |
35 | org.hamcrest
36 | hamcrest-library
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 | org.apache.maven.plugins
45 | maven-jar-plugin
46 | 2.4
47 |
48 |
49 | org.apache.maven.plugins
50 | maven-release-plugin
51 | 2.3
52 |
53 |
54 |
55 |
56 |
57 |
58 | org.apache.maven.plugins
59 | maven-jar-plugin
60 |
61 |
62 |
63 | test-jar
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ferstl/maven/pomenforcers/model/BuildModel.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 - 2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.ferstl.maven.pomenforcers.model;
17 |
18 | import java.util.Collections;
19 | import java.util.List;
20 | import java.util.Objects;
21 | import javax.xml.bind.annotation.XmlElement;
22 | import com.google.common.base.Joiner;
23 |
24 | class BuildModel {
25 |
26 | private static final Joiner TO_STRING_JOINER = Joiner.on("\n");
27 |
28 | @XmlElement(namespace = "http://maven.apache.org/POM/4.0.0")
29 | private PluginManagementModel pluginManagement;
30 |
31 | @XmlElement(namespace = "http://maven.apache.org/POM/4.0.0")
32 | private PluginsModel plugins;
33 |
34 |
35 | public List getManagedPlugins() {
36 | return this.pluginManagement != null ? this.pluginManagement.getPlugins() : Collections.emptyList();
37 | }
38 |
39 | public List getPlugins() {
40 | return this.plugins != null ? this.plugins.getPlugins() : Collections.emptyList();
41 | }
42 |
43 | @Override
44 | public boolean equals(Object obj) {
45 | if (obj == this) {
46 | return true;
47 | }
48 | if (!(obj instanceof BuildModel)) {
49 | return false;
50 | }
51 |
52 | BuildModel other = (BuildModel) obj;
53 | return Objects.equals(this.pluginManagement, other.pluginManagement)
54 | && Objects.equals(this.plugins, other.plugins);
55 | }
56 |
57 | @Override
58 | public int hashCode() {
59 | return Objects.hash(this.pluginManagement, this.plugins);
60 | }
61 |
62 | @Override
63 | public String toString() {
64 | return TO_STRING_JOINER.join(this.pluginManagement, this.plugins);
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ferstl/maven/pomenforcers/util/EnforcerRuleUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 - 2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.ferstl.maven.pomenforcers.util;
17 |
18 | import java.util.regex.Matcher;
19 | import java.util.regex.Pattern;
20 | import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
21 | import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
22 | import com.google.common.base.Strings;
23 |
24 | public final class EnforcerRuleUtils {
25 |
26 | private static final Pattern PROPERTY_PATTERN = Pattern.compile("\\$\\{.*?}");
27 |
28 | public static String evaluateProperties(String input, ExpressionEvaluator helper) {
29 | if (!Strings.isNullOrEmpty(input)) {
30 | Matcher matcher = PROPERTY_PATTERN.matcher(input);
31 | StringBuffer substituted = new StringBuffer();
32 | while (matcher.find()) {
33 | String property = matcher.group();
34 | matcher.appendReplacement(substituted, evaluateStringProperty(property, helper));
35 | }
36 | matcher.appendTail(substituted);
37 | return substituted.toString();
38 | }
39 | return input;
40 | }
41 |
42 | private static String evaluateStringProperty(String property, ExpressionEvaluator helper) {
43 | try {
44 | return (String) helper.evaluate(property);
45 | } catch (ExpressionEvaluationException e) {
46 | throw new IllegalArgumentException("Unable to resolve property " + property);
47 | } catch (ClassCastException e) {
48 | throw new IllegalArgumentException("Property " + property + " does not evaluate to a String");
49 | }
50 | }
51 |
52 | private EnforcerRuleUtils() {
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ferstl/maven/pomenforcers/model/functions/PluginMatcher.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 - 2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.ferstl.maven.pomenforcers.model.functions;
17 |
18 | import java.util.Objects;
19 | import org.apache.maven.model.Plugin;
20 | import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
21 | import com.github.ferstl.maven.pomenforcers.model.PluginModel;
22 | import static com.github.ferstl.maven.pomenforcers.util.EnforcerRuleUtils.evaluateProperties;
23 | import static com.google.common.base.Strings.isNullOrEmpty;
24 |
25 | /**
26 | * Matches Maven {@link Plugin} objects with {@link PluginModel} objects.
27 | */
28 | public class PluginMatcher extends AbstractOneToOneMatcher {
29 |
30 | private static final String DEFAULT_GROUP_ID = "org.apache.maven.plugins";
31 |
32 | public PluginMatcher(ExpressionEvaluator helper) {
33 | super(helper);
34 | }
35 |
36 | @Override
37 | protected PluginModel transform(Plugin mavenPlugin) {
38 | return new PluginModel(mavenPlugin.getGroupId(), mavenPlugin.getArtifactId(), mavenPlugin.getVersion());
39 | }
40 |
41 | @Override
42 | protected boolean matches(PluginModel supersetItem, PluginModel subsetItem) {
43 | String groupId = getGroupId(subsetItem);
44 | String artifactId = evaluateProperties(subsetItem.getArtifactId(), getHelper());
45 |
46 | return Objects.equals(supersetItem.getGroupId(), groupId)
47 | && Objects.equals(supersetItem.getArtifactId(), artifactId);
48 | }
49 |
50 | private String getGroupId(PluginModel plugin) {
51 | String groupId = evaluateProperties(plugin.getGroupId(), getHelper());
52 | return !isNullOrEmpty(groupId) ? groupId : DEFAULT_GROUP_ID;
53 | }
54 |
55 | }
56 |
--------------------------------------------------------------------------------
/src/test/projects/warn-only/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 |
6 | Integration test that test the warnOnly flag. Note that this section is at the wrong place according to the
7 | Pedantic POM Section Order Enforcer.
8 |
9 |
10 | com.github.ferstl
11 | warn-only
12 | 1.0-SNAPSHOT
13 |
14 |
15 |
16 |
17 | junit
18 | junit
19 | 4.13.2
20 | test
21 |
22 |
23 | org.apache.commons
24 | commons-lang3
25 | 3.3.2
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 | org.apache.maven.plugins
34 | maven-enforcer-plugin
35 | 3.6.0
36 |
37 |
38 |
39 | true
40 |
41 |
42 | true
43 |
44 |
45 |
46 |
47 |
48 | com.github.ferstl
49 | pedantic-pom-enforcers
50 | ${it-plugin.version}
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 | org.apache.maven.plugins
59 | maven-enforcer-plugin
60 |
61 |
62 |
63 |
64 |
--------------------------------------------------------------------------------
/src/test/projects/simple-project/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 | com.github.ferstl
6 | simple-project-it
7 | 1.0-SNAPSHOT
8 |
9 |
10 | Integration test which verifies that the enforcer rules work with the simplest possible maven project.
11 |
12 |
13 |
14 |
15 |
16 |
17 | org.apache.maven.plugins
18 | maven-enforcer-plugin
19 | 3.6.0
20 |
21 |
22 |
23 |
24 |
25 | POM_SECTION_ORDER,MODULE_ORDER,DEPENDENCY_MANAGEMENT_ORDER,DEPENDENCY_ORDER,DEPENDENCY_SCOPE,DEPENDENCY_CONFIGURATION,DEPENDENCY_MANAGEMENT_LOCATION,PLUGIN_MANAGEMENT_ORDER,PLUGIN_CONFIGURATION,PLUGIN_MANAGEMENT_LOCATION
26 |
27 |
28 |
30 | com.github.ferstl:simple-project-it
31 | com.github.ferstl:simple-project-it
32 |
33 |
34 | true
35 |
36 |
37 |
38 | com.github.ferstl
39 | pedantic-pom-enforcers
40 | ${it-plugin.version}
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 | org.apache.maven.plugins
49 | maven-enforcer-plugin
50 |
51 |
52 |
53 |
54 |
--------------------------------------------------------------------------------
/src/test/projects/issue-23/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 | com.github.ferstl
6 | issue-23
7 | 1.0-SNAPSHOT
8 |
9 |
10 | Integration test which verifies that the enforcer rules work with the simplest possible maven project.
11 |
12 |
13 |
14 |
15 |
16 | org.springframework
17 | spring-jdbc
18 | 5.1.3.RELEASE
19 |
20 |
21 |
22 |
23 |
24 |
25 | org.springframework
26 | spring-jdbc
27 |
28 |
29 | *
30 | *
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 | org.apache.maven.plugins
41 | maven-enforcer-plugin
42 | 3.6.0
43 |
44 |
45 |
46 |
47 |
48 | DEPENDENCY_CONFIGURATION
49 |
50 |
51 |
52 | true
53 |
54 |
55 |
56 | com.github.ferstl
57 | pedantic-pom-enforcers
58 | ${it-plugin.version}
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 | org.apache.maven.plugins
67 | maven-enforcer-plugin
68 |
69 |
70 |
71 |
72 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ferstl/maven/pomenforcers/model/PluginModel.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 - 2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.ferstl.maven.pomenforcers.model;
17 |
18 | import java.util.Collections;
19 | import java.util.List;
20 | import java.util.Objects;
21 | import javax.xml.bind.annotation.XmlAnyElement;
22 | import javax.xml.bind.annotation.XmlElement;
23 | import javax.xml.bind.annotation.XmlElementWrapper;
24 | import org.w3c.dom.Element;
25 |
26 | public class PluginModel extends ArtifactModel {
27 |
28 | @XmlElementWrapper(name = "configuration", namespace = "http://maven.apache.org/POM/4.0.0")
29 | @XmlAnyElement
30 | private List configItems;
31 |
32 | @XmlElement(namespace = "http://maven.apache.org/POM/4.0.0")
33 | private DependenciesModel dependencies;
34 |
35 | PluginModel() {
36 | }
37 |
38 | public PluginModel(String groupId, String artifactId, String version) {
39 | super(groupId, artifactId, version);
40 | }
41 |
42 | public boolean isConfigured() {
43 | return this.configItems != null && !this.configItems.isEmpty();
44 | }
45 |
46 | public List getDependencies() {
47 | return this.dependencies != null ? this.dependencies.getDependencies() : Collections.emptyList();
48 | }
49 |
50 | @Override
51 | public boolean equals(Object obj) {
52 | if (obj == this) {
53 | return true;
54 | }
55 | if (!(obj instanceof PluginModel)) {
56 | return false;
57 | }
58 |
59 | PluginModel other = (PluginModel) obj;
60 | return super.equals(other)
61 | // TODO: Element implementations may not implement equals()!!
62 | && Objects.equals(this.configItems, other.configItems)
63 | && Objects.equals(this.dependencies, other.dependencies);
64 | }
65 |
66 | @Override
67 | public int hashCode() {
68 | return Objects.hash(super.hashCode(), this.configItems, this.dependencies);
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/src/test/java/com/github/ferstl/maven/pomenforcers/priority/PriorityOrderingTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 - 2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.ferstl.maven.pomenforcers.priority;
17 |
18 | import java.util.ArrayList;
19 | import java.util.function.Function;
20 | import org.junit.jupiter.api.Test;
21 | import com.google.common.collect.Lists;
22 | import static org.assertj.core.api.Assertions.assertThat;
23 |
24 |
25 | class PriorityOrderingTest {
26 |
27 |
28 | @Test
29 | void testCompare() {
30 | ArrayList prioritizedItems = Lists.newArrayList("z", "y", "x");
31 | Function transformer = Function.identity();
32 | PriorityOrdering testComparator = new PriorityOrdering<>(prioritizedItems, transformer);
33 |
34 | // x is in the priority list, a isn't -> a > x
35 | assertThat(testComparator.compare("a", "x")).isGreaterThan(0);
36 | // x is located after y in the priority list -> x > y
37 | assertThat(testComparator.compare("x", "y")).isGreaterThan(0);
38 | // x is located after y in the priority list -> y < x
39 | assertThat(testComparator.compare("y", "x")).isLessThan(0);
40 | // equality applies to values in the priority list
41 | assertThat(testComparator.compare("x", "x")).isEqualTo(0);
42 | // regular comparison for values that are not in the priority list
43 | assertThat(testComparator.compare("b", "c")).isLessThan(0);
44 | assertThat(testComparator.compare("b", "b")).isEqualTo(0);
45 | assertThat(testComparator.compare("c", "b")).isGreaterThan(0);
46 | }
47 |
48 | @Test
49 | void testCompareWithoutPriorities() {
50 | ArrayList prioritizedItems = Lists.newArrayList();
51 | Function identity = Function.identity();
52 | PriorityOrdering testComparator = new PriorityOrdering<>(prioritizedItems, identity);
53 |
54 | assertThat(testComparator.compare("a", "b")).isLessThan(0);
55 | assertThat(testComparator.compare("a", "a")).isEqualTo(0);
56 | assertThat(testComparator.compare("b", "a")).isGreaterThan(0);
57 | }
58 |
59 |
60 | }
61 |
--------------------------------------------------------------------------------
/.github/workflows/codeql-analysis.yml:
--------------------------------------------------------------------------------
1 | # For most projects, this workflow file will not need changing; you simply need
2 | # to commit it to your repository.
3 | #
4 | # You may wish to alter this file to override the set of languages analyzed,
5 | # or to provide custom queries or build logic.
6 | #
7 | # ******** NOTE ********
8 | # We have attempted to detect the languages in your repository. Please check
9 | # the `language` matrix defined below to confirm you have the correct set of
10 | # supported CodeQL languages.
11 | #
12 | name: "CodeQL"
13 |
14 | on:
15 | push:
16 | branches: [ master ]
17 | pull_request:
18 | # The branches below must be a subset of the branches above
19 | branches: [ master ]
20 | workflow_dispatch:
21 | schedule:
22 | - cron: '38 17 * * 6'
23 |
24 | jobs:
25 | analyze:
26 | name: Analyze
27 | runs-on: ubuntu-latest
28 | permissions:
29 | actions: read
30 | contents: read
31 | security-events: write
32 |
33 | strategy:
34 | fail-fast: false
35 | matrix:
36 | language: [ 'java' ]
37 | # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
38 | # Learn more:
39 | # https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed
40 |
41 | steps:
42 | - name: Checkout repository
43 | uses: actions/checkout@v2
44 |
45 | # Initializes the CodeQL tools for scanning.
46 | - name: Initialize CodeQL
47 | uses: github/codeql-action/init@v1
48 | with:
49 | languages: ${{ matrix.language }}
50 | # If you wish to specify custom queries, you can do so here or in a config file.
51 | # By default, queries listed here will override any specified in a config file.
52 | # Prefix the list here with "+" to use these queries and those in the config file.
53 | # queries: ./path/to/local/query, your-org/your-repo/queries@main
54 |
55 | # Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
56 | # If this step fails, then you should remove it and run the build manually (see below)
57 | - name: Autobuild
58 | uses: github/codeql-action/autobuild@v1
59 |
60 | # ℹ️ Command-line programs to run using the OS shell.
61 | # 📚 https://git.io/JvXDl
62 |
63 | # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
64 | # and modify them (or add more) to build your code if your project
65 | # uses a compiled language
66 |
67 | #- run: |
68 | # make bootstrap
69 | # make release
70 |
71 | - name: Perform CodeQL Analysis
72 | uses: github/codeql-action/analyze@v1
73 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ferstl/maven/pomenforcers/model/PomSection.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 - 2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.ferstl.maven.pomenforcers.model;
17 |
18 | import java.util.Map;
19 | import com.google.common.collect.Maps;
20 | import static java.util.Objects.requireNonNull;
21 |
22 |
23 | public enum PomSection {
24 | MODEL_VERSION("modelVersion"),
25 | PREREQUISITES("prerequisites"),
26 | PARENT("parent"),
27 | GROUP_ID("groupId"),
28 | ARTIFACT_ID("artifactId"),
29 | VERSION("version"),
30 | PACKAGING("packaging"),
31 | NAME("name"),
32 | DESCRIPTION("description"),
33 | URL("url"),
34 | LICENSES("licenses"),
35 | ORGANIZATION("organization"),
36 | INCEPTION_YEAR("inceptionYear"),
37 | CI_MANAGEMENT("ciManagement"),
38 | MAILING_LISTS("mailingLists"),
39 | ISSUE_MANAGEMENT("issueManagement"),
40 | DEVELOPERS("developers"),
41 | CONTRIBUTORS("contributors"),
42 | SCM("scm"),
43 | REPOSITORIES("repositories"),
44 | PLUGIN_REPOSITORIES("pluginRepositories"),
45 | DISTRIBUTION_MANAGEMENT("distributionManagement"),
46 | MODULES("modules"),
47 | PROPERTIES("properties"),
48 | DEPENDENCY_MANAGEMENT("dependencyManagement"),
49 | DEPENDENCIES("dependencies"),
50 | BUILD("build"),
51 | PROFILES("profiles"),
52 | REPORTING("reporting"),
53 | REPORTS("reports");
54 |
55 | private static final Map pomSectionMap;
56 |
57 | static {
58 | pomSectionMap = Maps.newHashMap();
59 | for (PomSection pomSection : values()) {
60 | pomSectionMap.put(pomSection.getSectionName(), pomSection);
61 | }
62 | }
63 |
64 | public static PomSection getBySectionName(String sectionName) {
65 | requireNonNull(sectionName, "Section name is null.");
66 |
67 | PomSection value = pomSectionMap.get(sectionName);
68 | if (value == null) {
69 | throw new IllegalArgumentException("POM section " + sectionName + " does not exist.");
70 | }
71 |
72 | return value;
73 | }
74 |
75 | private final String sectionName;
76 |
77 | PomSection(String sectionName) {
78 | this.sectionName = sectionName;
79 | }
80 |
81 | public String getSectionName() {
82 | return this.sectionName;
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ferstl/maven/pomenforcers/model/functions/AbstractOneToOneMatcher.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 - 2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.ferstl.maven.pomenforcers.model.functions;
17 |
18 | import java.util.ArrayList;
19 | import java.util.Collection;
20 | import java.util.List;
21 | import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
22 | import com.google.common.collect.BiMap;
23 | import com.google.common.collect.ImmutableBiMap;
24 | import com.google.common.collect.ImmutableBiMap.Builder;
25 |
26 |
27 | public abstract class AbstractOneToOneMatcher {
28 |
29 | private final ExpressionEvaluator helper;
30 |
31 | AbstractOneToOneMatcher(ExpressionEvaluator helper) {
32 | this.helper = helper;
33 | }
34 |
35 | public final BiMap match(Collection superset, Collection subset) {
36 | Builder mapBuilder = ImmutableBiMap.builder();
37 |
38 | // Transform the superset here in order not to do it in each nested loop
39 | Collection transformedSuperset = transformSuperset(superset);
40 |
41 | for (V subsetItem : subset) {
42 | boolean itemMatched = false;
43 |
44 | for (V supersetItem : transformedSuperset) {
45 | if (matches(supersetItem, subsetItem)) {
46 | itemMatched = true;
47 | mapBuilder.put(supersetItem, subsetItem);
48 | break;
49 | }
50 | }
51 |
52 | if (!itemMatched) {
53 | handleUnmatchedItem(mapBuilder, subsetItem);
54 | }
55 | }
56 |
57 | return mapBuilder.build();
58 | }
59 |
60 | protected void handleUnmatchedItem(Builder mapBuilder, V subsetItem) {
61 | throw new IllegalArgumentException("Could not match item " + subsetItem + " with superset");
62 | }
63 |
64 | protected abstract V transform(U supersetItem);
65 |
66 | protected abstract boolean matches(V supersetItem, V subsetItem);
67 |
68 | ExpressionEvaluator getHelper() {
69 | return this.helper;
70 | }
71 |
72 | private Collection transformSuperset(Collection superset) {
73 | List transformed = new ArrayList<>(superset.size());
74 | for (U supersetItem : superset) {
75 | transformed.add(transform(supersetItem));
76 | }
77 | return transformed;
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/src/test/java/com/github/ferstl/maven/pomenforcers/model/functions/PluginMatcherTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 - 2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.ferstl.maven.pomenforcers.model.functions;
17 |
18 | import org.apache.maven.model.Plugin;
19 | import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
20 | import org.junit.jupiter.api.BeforeEach;
21 | import org.junit.jupiter.api.Test;
22 | import com.github.ferstl.maven.pomenforcers.model.PluginModel;
23 | import static org.junit.jupiter.api.Assertions.assertEquals;
24 | import static org.junit.jupiter.api.Assertions.assertTrue;
25 | import static org.mockito.Mockito.mock;
26 |
27 | public class PluginMatcherTest {
28 |
29 | private PluginMatcher pluginMatcher;
30 |
31 | @BeforeEach
32 | public void before() {
33 | this.pluginMatcher = new PluginMatcher(mock(ExpressionEvaluator.class));
34 | }
35 |
36 | @Test
37 | public void transform() {
38 | // arrange
39 | Plugin plugin = new Plugin();
40 | plugin.setGroupId("a");
41 | plugin.setArtifactId("b");
42 | plugin.setVersion("c");
43 |
44 | // act
45 | PluginModel pluginModel = this.pluginMatcher.transform(plugin);
46 |
47 | // assert
48 | assertEquals("a", pluginModel.getGroupId());
49 | assertEquals("b", pluginModel.getArtifactId());
50 | assertEquals("c", pluginModel.getVersion());
51 | }
52 |
53 | @Test
54 | public void matchWithAllGavParameters() {
55 | PluginModel supersetPlugin = new PluginModel("a", "b", "c");
56 | PluginModel subsetPlugin = new PluginModel("a", "b", "c");
57 |
58 | assertTrue(this.pluginMatcher.matches(supersetPlugin, subsetPlugin));
59 | }
60 |
61 | @Test
62 | public void matchWithDefaultGroupIdForNull() {
63 | PluginModel supersetPlugin = new PluginModel("org.apache.maven.plugins", "b", "c");
64 | PluginModel subsetPlugin = new PluginModel(null, "b", "c");
65 |
66 | assertTrue(this.pluginMatcher.matches(supersetPlugin, subsetPlugin));
67 | }
68 |
69 | @Test
70 | public void matchWithDefaultGroupIdForEmpty() {
71 | PluginModel supersetPlugin = new PluginModel("org.apache.maven.plugins", "b", "c");
72 | PluginModel subsetPlugin = new PluginModel("", "b", "c");
73 |
74 | assertTrue(this.pluginMatcher.matches(supersetPlugin, subsetPlugin));
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ferstl/maven/pomenforcers/model/PluginElement.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 - 2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.ferstl.maven.pomenforcers.model;
17 |
18 | import java.util.Collection;
19 | import java.util.Map;
20 | import java.util.function.Function;
21 | import com.github.ferstl.maven.pomenforcers.priority.PriorityOrdering;
22 | import com.github.ferstl.maven.pomenforcers.priority.PriorityOrderingFactory;
23 | import com.google.common.collect.Maps;
24 | import static com.github.ferstl.maven.pomenforcers.model.functions.StringStartsWithEquivalence.stringStartsWith;
25 | import static java.util.Objects.requireNonNull;
26 |
27 | public enum PluginElement implements PriorityOrderingFactory, Function {
28 |
29 | GROUP_ID("groupId") {
30 | @Override
31 | public PriorityOrdering createPriorityOrdering(Collection priorityCollection) {
32 | return new PriorityOrdering<>(priorityCollection, this, stringStartsWith());
33 | }
34 |
35 | @Override
36 | public String apply(PluginModel input) {
37 | return input.getGroupId();
38 | }
39 | },
40 |
41 | ARTIFACT_ID("artifactId") {
42 | @Override
43 | public PriorityOrdering createPriorityOrdering(Collection priorityCollection) {
44 | return new PriorityOrdering<>(priorityCollection, this, stringStartsWith());
45 | }
46 |
47 | @Override
48 | public String apply(PluginModel input) {
49 | return input.getArtifactId();
50 | }
51 | };
52 |
53 | private static final Map elementMap;
54 |
55 | static {
56 | elementMap = Maps.newLinkedHashMap();
57 | for (PluginElement element : values()) {
58 | elementMap.put(element.getElementName(), element);
59 | }
60 | }
61 |
62 | private final String elementName;
63 |
64 | PluginElement(String elementName) {
65 | this.elementName = elementName;
66 | }
67 |
68 | public String getElementName() {
69 | return this.elementName;
70 | }
71 |
72 | public static PluginElement getByElementName(String elementName) {
73 | requireNonNull(elementName, "Element name is null");
74 |
75 | PluginElement result = elementMap.get(elementName);
76 | if (result == null) {
77 | throw new IllegalArgumentException("No plugin element with name " + elementName);
78 | }
79 |
80 | return result;
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ferstl/maven/pomenforcers/model/functions/DependencyMatcher.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 - 2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.ferstl.maven.pomenforcers.model.functions;
17 |
18 | import java.util.Objects;
19 | import org.apache.maven.model.Dependency;
20 | import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
21 | import com.github.ferstl.maven.pomenforcers.model.DependencyModel;
22 | import com.github.ferstl.maven.pomenforcers.model.DependencyScope;
23 | import com.google.common.collect.ImmutableBiMap.Builder;
24 | import static com.github.ferstl.maven.pomenforcers.util.EnforcerRuleUtils.evaluateProperties;
25 |
26 | /**
27 | * Matches Maven {@link Dependency} objects with {@link DependencyModel} objects.
28 | */
29 | public class DependencyMatcher extends AbstractOneToOneMatcher {
30 |
31 | public DependencyMatcher(ExpressionEvaluator helper) {
32 | super(helper);
33 | }
34 |
35 | @Override
36 | protected DependencyModel transform(Dependency mavenDependency) {
37 | return new DependencyModel(
38 | mavenDependency.getGroupId(),
39 | mavenDependency.getArtifactId(),
40 | mavenDependency.getVersion(),
41 | mavenDependency.getScope(),
42 | mavenDependency.getClassifier(),
43 | mavenDependency.getType());
44 | }
45 |
46 | @Override
47 | protected boolean matches(DependencyModel supersetItem, DependencyModel subsetItem) {
48 | String groupId = evaluateProperties(subsetItem.getGroupId(), getHelper());
49 | String artifactId = evaluateProperties(subsetItem.getArtifactId(), getHelper());
50 | String classifier = evaluateProperties(subsetItem.getClassifier(), getHelper());
51 | String type = evaluateProperties(subsetItem.getType(), getHelper());
52 |
53 | return Objects.equals(supersetItem.getGroupId(), groupId)
54 | && Objects.equals(supersetItem.getArtifactId(), artifactId)
55 | && Objects.equals(supersetItem.getClassifier(), classifier)
56 | && Objects.equals(supersetItem.getType(), type);
57 | }
58 |
59 | @Override
60 | protected void handleUnmatchedItem(
61 | Builder mapBuilder,
62 | DependencyModel subsetItem) {
63 | String type = evaluateProperties(subsetItem.getType(), getHelper());
64 | if ("pom".equals(type) && DependencyScope.IMPORT.equals(subsetItem.getScope())) {
65 | mapBuilder.put(subsetItem, subsetItem);
66 | } else {
67 | super.handleUnmatchedItem(mapBuilder, subsetItem);
68 | }
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/src/test/java/com/github/ferstl/maven/pomenforcers/PedanticModuleOrderEnforcerTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 - 2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.ferstl.maven.pomenforcers;
17 |
18 | import java.util.Arrays;
19 | import org.junit.jupiter.api.BeforeEach;
20 | import org.junit.jupiter.api.Test;
21 | import org.junit.jupiter.api.TestInstance;
22 | import org.junit.jupiter.api.TestInstance.Lifecycle;
23 | import static org.assertj.core.api.Assertions.assertThat;
24 | import static org.mockito.Mockito.mock;
25 | import static org.mockito.Mockito.verify;
26 | import static org.mockito.Mockito.when;
27 |
28 | /**
29 | * JUnit tests for {@link PedanticModuleOrderEnforcer}.
30 | */
31 | @TestInstance(Lifecycle.PER_CLASS)
32 | class PedanticModuleOrderEnforcerTest extends AbstractPedanticEnforcerTest {
33 |
34 | @Override
35 | PedanticModuleOrderEnforcer createRule() {
36 | return new PedanticModuleOrderEnforcer(this.mockMavenProject, this.mockHelper);
37 | }
38 |
39 | @BeforeEach
40 | void before() {
41 | when(this.mockMavenProject.getPackaging()).thenReturn("pom");
42 | }
43 |
44 | @Override
45 | @Test
46 | void getDescription() {
47 | assertThat(this.testRule.getDescription()).isEqualTo(PedanticEnforcerRule.MODULE_ORDER);
48 | }
49 |
50 | @Override
51 | @Test
52 | void accept() {
53 | PedanticEnforcerVisitor visitor = mock(PedanticEnforcerVisitor.class);
54 | this.testRule.accept(visitor);
55 |
56 | verify(visitor).visit(this.testRule);
57 | }
58 |
59 | @Test
60 | void correctOrder() {
61 | when(this.projectModel.getModules()).thenReturn(Arrays.asList("m1", "m2", "m3"));
62 |
63 | executeRuleAndCheckReport(false);
64 | }
65 |
66 | @Test
67 | void correctOrderWithIgnores() {
68 | when(this.projectModel.getModules()).thenReturn(Arrays.asList("m9", "m8", "m1", "m2", "m7", "m3"));
69 | this.testRule.setIgnoredModules("m9,m8,m7");
70 |
71 | executeRuleAndCheckReport(false);
72 | }
73 |
74 | @Test
75 | void noPomPackaging() {
76 | when(this.mockMavenProject.getPackaging()).thenReturn("jar");
77 | when(this.projectModel.getModules()).thenReturn(null);
78 |
79 | executeRuleAndCheckReport(false);
80 | }
81 |
82 | @Test
83 | void incorrectOrder() {
84 | when(this.projectModel.getModules()).thenReturn(Arrays.asList("m2", "m1"));
85 |
86 | executeRuleAndCheckReport(true);
87 | }
88 |
89 | @Test
90 | void incorrectOrderWithIgnores() {
91 | when(this.projectModel.getModules()).thenReturn(Arrays.asList("m9", "m2", "m1"));
92 | this.testRule.setIgnoredModules("m9");
93 |
94 | executeRuleAndCheckReport(true);
95 | }
96 |
97 | }
98 |
--------------------------------------------------------------------------------
/src/test/java/com/github/ferstl/maven/pomenforcers/PedanticPluginManagementLocationEnforcerTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 - 2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.ferstl.maven.pomenforcers;
17 |
18 | import org.junit.jupiter.api.BeforeEach;
19 | import org.junit.jupiter.api.Test;
20 | import com.github.ferstl.maven.pomenforcers.model.PluginModel;
21 | import static org.assertj.core.api.Assertions.assertThat;
22 | import static org.mockito.Mockito.mock;
23 | import static org.mockito.Mockito.verify;
24 | import static org.mockito.Mockito.when;
25 |
26 | /**
27 | * JUnit tests for {@link PedanticPluginManagementLocationEnforcer}.
28 | */
29 | class PedanticPluginManagementLocationEnforcerTest extends AbstractPedanticEnforcerTest {
30 |
31 | @Override
32 | PedanticPluginManagementLocationEnforcer createRule() {
33 | return new PedanticPluginManagementLocationEnforcer(this.mockMavenProject, this.mockHelper);
34 | }
35 |
36 | @BeforeEach
37 | void before() {
38 | when(this.mockMavenProject.getGroupId()).thenReturn("a.b.c");
39 | when(this.mockMavenProject.getArtifactId()).thenReturn("parent");
40 | this.projectModel.getManagedPlugins().add(new PluginModel("a.b.c", "a", "1.0"));
41 | }
42 |
43 | @Override
44 | @Test
45 | void getDescription() {
46 | assertThat(this.testRule.getDescription()).isEqualTo(PedanticEnforcerRule.PLUGIN_MANAGEMENT_LOCATION);
47 | }
48 |
49 | @Override
50 | @Test
51 | void accept() {
52 | PedanticEnforcerVisitor visitor = mock(PedanticEnforcerVisitor.class);
53 | this.testRule.accept(visitor);
54 |
55 | verify(visitor).visit(this.testRule);
56 | }
57 |
58 | @Test
59 | void noPluginManagingPomsDeclared() {
60 | executeRuleAndCheckReport(false);
61 | }
62 |
63 | @Test
64 | void isPluginManagingPom() {
65 | this.testRule.setPluginManagingPoms("a.b.c:parent");
66 |
67 | executeRuleAndCheckReport(false);
68 | }
69 |
70 | @Test
71 | void isNotPluginManagingPom() {
72 | this.testRule.setPluginManagingPoms("some.other:pom");
73 |
74 | executeRuleAndCheckReport(true);
75 | }
76 |
77 | void pluginManagementAllowedInParentPom() {
78 | when(this.mockMavenProject.getPackaging()).thenReturn("pom");
79 |
80 | executeRuleAndCheckReport(true);
81 | }
82 |
83 | void pluginManagementNotAllowedInParentPom() {
84 | when(this.mockMavenProject.getPackaging()).thenReturn("pom");
85 | this.testRule.setAllowParentPoms(false);
86 |
87 | executeRuleAndCheckReport(true);
88 | }
89 |
90 | @Test
91 | void pluginManagementInNonParentPom() {
92 | when(this.mockMavenProject.getPackaging()).thenReturn("jar");
93 |
94 | executeRuleAndCheckReport(false);
95 | }
96 | }
97 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ferstl/maven/pomenforcers/priority/CompoundPriorityOrdering.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 - 2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.ferstl.maven.pomenforcers.priority;
17 |
18 | import java.util.ArrayList;
19 | import java.util.Arrays;
20 | import java.util.Comparator;
21 | import java.util.List;
22 | import java.util.Set;
23 | import com.google.common.collect.Iterables;
24 | import com.google.common.collect.LinkedHashMultimap;
25 | import com.google.common.collect.Lists;
26 | import com.google.common.collect.Multimap;
27 | import com.google.common.collect.Ordering;
28 | import com.google.common.collect.Sets;
29 |
30 | /**
31 | * @param Type of this ordering.
32 | * @param
Type of the priorities.
33 | * @param Type of the {@link PriorityOrderingFactory}.
34 | */
35 | public class CompoundPriorityOrdering, F extends PriorityOrderingFactory
> extends Ordering {
36 |
37 | private final Set orderBy;
38 | private final Multimap priorityMap;
39 |
40 | public static , F extends PriorityOrderingFactory
> CompoundPriorityOrdering orderBy(Iterable artifactElements) {
41 | if (Iterables.isEmpty(artifactElements)) {
42 | throw new IllegalArgumentException("No order specified.");
43 | }
44 | return new CompoundPriorityOrdering<>(artifactElements);
45 | }
46 |
47 | @SafeVarargs
48 | public static , F extends PriorityOrderingFactory
priorityCollection;
42 |
43 | /**
44 | * Matches the values to be compared with the items in the priority collection.
45 | */
46 | private final Equivalence super P> priorityMatcher;
47 |
48 | /**
49 | * Transforms the type of the objects to be compared into the type of the priority collection. Use
50 | * {@link Function#identity()} if the type of the priority collection and the type of the objects to be
51 | * compared are the same.
52 | */
53 | private final Function transformer;
54 |
55 |
56 | public PriorityOrdering(Collection
priorityCollection, Function transformer) {
63 | this(priorityCollection, transformer, Equivalence.equals());
64 | }
65 |
66 | @Override
67 | public int compare(T object1, T object2) {
68 | P comparable1 = this.transformer.apply(object1);
69 | P comparable2 = this.transformer.apply(object2);
70 |
71 | int rank1 = this.rank(comparable1);
72 | int rank2 = this.rank(comparable2);
73 |
74 | if (rank1 == rank2) {
75 | return comparable1.compareTo(comparable2);
76 | }
77 |
78 | return rank1 - rank2;
79 |
80 | }
81 |
82 | /**
83 | * Determine the priority of the given item by matching it against the priority collection.
84 | * The lower the rank, the higher the priority.
85 | *
86 | * @param item The item to prioritize.
87 | * @return The priority of the given item or {@link Integer#MAX_VALUE} if the given item does not
88 | * match any element of the priority collection.
89 | */
90 | private int rank(P item) {
91 | int i = 0;
92 | for (P prioritizedItem : this.priorityCollection) {
93 | if (this.priorityMatcher.equivalent(item, prioritizedItem)) {
94 | return i;
95 | }
96 | i++;
97 | }
98 |
99 | return Integer.MAX_VALUE;
100 | }
101 |
102 | }
103 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ferstl/maven/pomenforcers/PedanticDependencyManagementOrderEnforcer.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 - 2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.ferstl.maven.pomenforcers;
17 |
18 | import java.util.Collection;
19 | import java.util.Collections;
20 | import javax.inject.Inject;
21 | import javax.inject.Named;
22 | import org.apache.maven.model.Dependency;
23 | import org.apache.maven.model.DependencyManagement;
24 | import org.apache.maven.project.MavenProject;
25 | import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
26 | import com.github.ferstl.maven.pomenforcers.model.DependencyModel;
27 |
28 |
29 | /**
30 | * This enforcer makes sure that all artifacts in your dependency management are
31 | * ordered. The ordering can be defined by any combination of scope,
32 | * groupId and artifactId. Each of these attributes
33 | * may be given a priority.
34 | *
35 | * ### Example
36 | * <rules>
37 | * <dependencyManagementOrder implementation="com.github.ferstl.maven.pomenforcers.PedanticDependencyManagementOrderEnforcer">
38 | * <!-- order by scope, groupId and artifactId (default) -->
39 | * <orderBy>scope,groupId,artifactId</orderBy>
40 | * <!-- runtime scope should occur before provided scope -->
41 | * <scopePriorities>compile,runtime,provided</scopePriorities>
42 | * <!-- all group IDs starting with com.myproject and com.mylibs should occur first -->
43 | * <groupIdPriorities>com.myproject,com.mylibs</groupIdPriorities>
44 | * <!-- all artifact IDs starting with commons- and utils- should occur first -->
45 | * <artifactIdPriorities>commons-,utils-</artifactIdPriorities>
46 | * </dependencyManagementOrder>
47 | * </rules>
48 | *
49 | *
50 | * @id {@link PedanticEnforcerRule#DEPENDENCY_MANAGEMENT_ORDER}
51 | * @since 1.0.0
52 | */
53 | @Named("dependencyManagementOrder")
54 | public class PedanticDependencyManagementOrderEnforcer extends AbstractPedanticDependencyOrderEnforcer {
55 |
56 | @Inject
57 | public PedanticDependencyManagementOrderEnforcer(final MavenProject project, final ExpressionEvaluator helper) {
58 | super(project, helper);
59 | }
60 |
61 | @Override
62 | protected PedanticEnforcerRule getDescription() {
63 | return PedanticEnforcerRule.DEPENDENCY_MANAGEMENT_ORDER;
64 | }
65 |
66 | @Override
67 | protected void accept(PedanticEnforcerVisitor visitor) {
68 | visitor.visit(this);
69 | }
70 |
71 | @Override
72 | protected Collection getDeclaredDependencies() {
73 | return getProjectModel().getManagedDependencies();
74 | }
75 |
76 | @Override
77 | protected Collection getMavenDependencies(MavenProject project) {
78 | DependencyManagement dependencyManagement = project.getDependencyManagement();
79 | if (dependencyManagement != null) {
80 | return dependencyManagement.getDependencies();
81 | } else {
82 | return Collections.emptyList();
83 | }
84 | }
85 |
86 | @Override
87 | protected void reportError(ErrorReport report, Collection resolvedDependencies, Collection sortedDependencies) {
88 |
89 | report.addLine("Your dependency management has to be ordered this way:")
90 | .emptyLine()
91 | .addDiffUsingToString(resolvedDependencies, sortedDependencies, "Actual Order", "Required Order");
92 | }
93 | }
94 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ferstl/maven/pomenforcers/model/ProjectModel.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 - 2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.ferstl.maven.pomenforcers.model;
17 |
18 | import java.util.Collections;
19 | import java.util.List;
20 | import java.util.Objects;
21 | import javax.xml.bind.annotation.XmlElement;
22 | import javax.xml.bind.annotation.XmlElementWrapper;
23 | import javax.xml.bind.annotation.XmlRootElement;
24 | import com.google.common.base.Joiner;
25 |
26 | @XmlRootElement(name = "project")
27 | public class ProjectModel {
28 |
29 | private static final Joiner TO_STRING_JOINER = Joiner.on("\n").skipNulls();
30 |
31 | @XmlElement(namespace = "http://maven.apache.org/POM/4.0.0")
32 | public String groupId;
33 | @XmlElement(namespace = "http://maven.apache.org/POM/4.0.0")
34 | public String artifactId;
35 |
36 | @XmlElementWrapper(namespace = "http://maven.apache.org/POM/4.0.0")
37 | @XmlElement(name = "module", namespace = "http://maven.apache.org/POM/4.0.0")
38 | public List modules;
39 |
40 | @XmlElement(namespace = "http://maven.apache.org/POM/4.0.0")
41 | public DependencyManagementModel dependencyManagement;
42 |
43 | @XmlElement(namespace = "http://maven.apache.org/POM/4.0.0")
44 | public DependenciesModel dependencies;
45 |
46 | @XmlElement(namespace = "http://maven.apache.org/POM/4.0.0")
47 | public BuildModel build;
48 |
49 | public List getModules() {
50 | return this.modules != null ? this.modules : Collections.emptyList();
51 | }
52 |
53 | public List getManagedDependencies() {
54 | return this.dependencyManagement != null ?
55 | this.dependencyManagement.getDependencies() : Collections.emptyList();
56 | }
57 |
58 | public List getDependencies() {
59 | return this.dependencies != null ?
60 | this.dependencies.getDependencies() : Collections.emptyList();
61 | }
62 |
63 | public List getManagedPlugins() {
64 | return this.build != null ? this.build.getManagedPlugins() : Collections.emptyList();
65 | }
66 |
67 | public List getPlugins() {
68 | return this.build != null ? this.build.getPlugins() : Collections.emptyList();
69 | }
70 |
71 | @Override
72 | public String toString() {
73 | StringBuilder sb = new StringBuilder("Project ")
74 | .append(this.groupId)
75 | .append(":")
76 | .append(this.artifactId)
77 | .append(" [\n");
78 | return TO_STRING_JOINER
79 | .appendTo(
80 | sb,
81 | CollectionToStringHelper.toString("Modules", this.modules),
82 | this.dependencyManagement,
83 | this.dependencies,
84 | this.build)
85 | .append("\n]")
86 | .toString();
87 | }
88 |
89 | @Override
90 | public boolean equals(Object obj) {
91 | if (obj == this) {
92 | return true;
93 | }
94 | if (!(obj instanceof ProjectModel)) {
95 | return false;
96 | }
97 | ProjectModel other = (ProjectModel) obj;
98 | return Objects.equals(this.groupId, other.groupId)
99 | && Objects.equals(this.artifactId, other.artifactId)
100 | && Objects.equals(this.modules, other.modules)
101 | && Objects.equals(this.dependencyManagement, other.dependencyManagement)
102 | && Objects.equals(this.dependencies, other.dependencies)
103 | && Objects.equals(this.build, other.build);
104 | }
105 |
106 | @Override
107 | public int hashCode() {
108 | return Objects.hash(
109 | this.groupId, this.artifactId, this.modules, this.dependencyManagement, this.dependencies, this.build);
110 | }
111 | }
112 |
--------------------------------------------------------------------------------
/src/main/java/com/github/ferstl/maven/pomenforcers/ErrorReport.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 - 2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.ferstl.maven.pomenforcers;
17 |
18 | import java.util.Collection;
19 | import java.util.LinkedList;
20 | import java.util.function.Function;
21 | import java.util.stream.Collectors;
22 | import com.github.ferstl.maven.pomenforcers.util.SideBySideDiffUtil;
23 | import com.google.common.base.Joiner;
24 | import com.google.common.base.Strings;
25 | import com.google.common.collect.Collections2;
26 | import static com.google.common.base.Functions.toStringFunction;
27 |
28 |
29 | public class ErrorReport {
30 |
31 | private static final String LIST_ITEM = "- ";
32 | private static final String LINE_SEPARATOR = System.getProperty("line.separator", "\n");
33 | private static final Joiner LINE_JOINER = Joiner.on(LINE_SEPARATOR);
34 | private static final Joiner LIST_JOINER = Joiner.on(LINE_SEPARATOR + LIST_ITEM);
35 |
36 | private final String title;
37 | private final Collection