Skip to content

Commit 45af527

Browse files
committed
Reuse pmd binary from pmd-dist/target/pmd-bin...zip if available
This is important for speeding up CI builds to avoid rebuilding PMD.
1 parent 2737ef0 commit 45af527

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

lib/pmdtester/builders/pmd_report_builder.rb

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,26 @@ def get_pmd_binary_file
4949

5050
# builds pmd on currently checked out branch
5151
def build_pmd(into_dir:)
52-
logger.info "#{@pmd_branch_name}: Building PMD #{@pmd_version}..."
53-
package_cmd = './mvnw clean package' \
54-
' -Dmaven.test.skip=true' \
55-
' -Dmaven.javadoc.skip=true' \
56-
' -Dmaven.source.skip=true' \
57-
' -Dcheckstyle.skip=true'
58-
Cmd.execute(package_cmd)
52+
# in CI there might have been a build performed already. In that case
53+
# we reuse the build result, otherwise we build PMD freshly
54+
pmd_dist_target = "pmd-dist/target/pmd-bin-#{@pmd_version}.zip"
55+
if File.exist?("#{@local_git_repo}/#{pmd_dist_target}")
56+
# that's a warning, because we don't know, whether this build really
57+
# belongs to the current branch or whether it's from a previous branch.
58+
# In CI, that's not a problem, because the workspace is always fresh.
59+
logger.warn "#{@pmd_branch_name}: Reusing already existing #{pmd_dist_target}"
60+
else
61+
logger.info "#{@pmd_branch_name}: Building PMD #{@pmd_version}..."
62+
package_cmd = './mvnw clean package' \
63+
' -Dmaven.test.skip=true' \
64+
' -Dmaven.javadoc.skip=true' \
65+
' -Dmaven.source.skip=true' \
66+
' -Dcheckstyle.skip=true'
67+
Cmd.execute(package_cmd)
68+
end
5969

6070
logger.info "#{@pmd_branch_name}: Extracting the zip"
61-
Cmd.execute("unzip -qo pmd-dist/target/pmd-bin-#{@pmd_version}.zip" \
62-
' -d pmd-dist/target/exploded')
71+
Cmd.execute("unzip -qo #{pmd_dist_target} -d pmd-dist/target/exploded")
6372
Cmd.execute("mv pmd-dist/target/exploded/pmd-bin-#{@pmd_version} #{into_dir}")
6473
end
6574

test/test_pmd_report_builder.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ class TestPmdReportBuilder < Test::Unit::TestCase
77
def setup
88
# pmd version that is simulated in tests when pmd should be built
99
@pmd_version = '6.10.0-SNAPSHOT'
10+
11+
# pre-built PMD binary
12+
pmd_binary = "target/repositories/pmd/pmd-dist/target/pmd-bin-#{@pmd_version}.zip"
13+
14+
File.unlink pmd_binary if File.exist? pmd_binary
1015
end
1116

1217
def test_build_skip
@@ -23,6 +28,31 @@ def test_build_skip
2328
.build
2429
end
2530

31+
# In CI, there is no previous existing distro that can be reused,
32+
# that means target/pmd-bin-6.10.0-SNAPSHOT-master-sha1abc does not
33+
# exist. However, pmd-dist/target/pmd-bin-6.10.0-SNAPSHOT.zip exists
34+
# from a previous build and should be reused.
35+
def test_build_skip_ci
36+
projects = []
37+
argv = %w[-r target/repositories/pmd -b master -p pmd_releases/6.1.0
38+
-c config/design.xml -l test/resources/project-test.xml]
39+
options = PmdTester::Options.new(argv)
40+
41+
FileUtils.mkdir_p 'target/repositories/pmd/pmd-dist/target'
42+
FileUtils.touch "target/repositories/pmd/pmd-dist/target/pmd-bin-#{@pmd_version}.zip"
43+
44+
record_expectations('sha1abc', 'sha1abc', false)
45+
PmdTester::Cmd.stubs(:execute).with("unzip -qo pmd-dist/target/pmd-bin-#{@pmd_version}.zip" \
46+
' -d pmd-dist/target/exploded').once
47+
PmdTester::Cmd.stubs(:execute).with("mv pmd-dist/target/exploded/pmd-bin-#{@pmd_version}" \
48+
" #{Dir.getwd}/target/pmd-bin-#{@pmd_version}-master-sha1abc").once
49+
record_expectations_after_build
50+
51+
PmdTester::PmdReportBuilder
52+
.new(projects, options, options.base_config, options.base_branch)
53+
.build
54+
end
55+
2656
def test_build_normal
2757
projects = []
2858
argv = %w[-r target/repositories/pmd -b master -p pmd_releases/6.1.0

0 commit comments

Comments
 (0)