開發系統我主要用的是Java,常會遇到系統移植的情況,比如拿A的Gradle專案變成B的Maven專案,Gradle與Maven互轉工具不太好找,而且我比較熟Maven,所以常用Perl來轉。
在gralde專案下指令匯出dependency的jar清單:
gradle dependencies > tree.txt
gradle dependencies會將相依jar依樹狀圖output,而且還會分compiler、runtimte、test各種情況相依jar。我就選compile部份的樹狀文字,用Perl轉成pom.xml
tree.txt內容摘錄如下:
compileClasspath - Compile classpath for source set 'main'.
+--- org.projectlombok:lombok -> 1.18.24
+--- org.springframework.boot:spring-boot-starter-data-jpa -> 2.7.5
| +--- org.springframework.boot:spring-boot-starter-aop:2.7.5
| | +--- org.springframework.boot:spring-boot-starter:2.7.5
| | | +--- org.springframework.boot:spring-boot:2.7.5
| | | | +--- org.springframework:spring-core:5.3.23
| | | | | \--- org.springframework:spring-jcl:5.3.23
| | | | \--- org.springframework:spring-context:5.3.23
而output來的jar有不少重複,由Perl去掉重複,將tree.txt內容放在perl的__DATA__之後,再用>方式產出dependency內容,例:./gradle2pom.pl > dependency.pom
#!/usr/bin/perl
my %depends;
while (<DATA>) {
chomp;
my @tags = split/\:/;
unless (exists $depends{$tags[0] . ":" . $tags[1]}) {
$depends{$tags[0] . ":" . $tags[1]} = 1;
my $pom = <<EOF;
<dependency>
<groupId>$tags[0]</groupId>
<artifactId>$tags[1]</artifactId>
<version>$tags[2]</version>
</dependency>
EOF
print $pom;
}
}
__DATA__
org.projectlombok:lombok:1.18.24
org.springframework.boot:spring-boot-starter-data-jpa:2.7.5
org.springframework.boot:spring-boot-starter-aop:2.7.5
org.springframework.boot:spring-boot-starter:2.7.5
org.springframework.boot:spring-boot:2.7.5
org.springframework:spring-core:5.3.23
org.springframework:spring-jcl:5.3.23
在客戶端不能上網,可能也沒架Jenkins之類,從開發端主機的repository將相依的jar連同目錄結構一併copy出來交付。一樣也是將tree.txt內容放在__DATA__之後,output內容再壓縮放,放在客戶端包版機的.m2/repository。
#!/usr/bin/perl
while (<DATA>) {
chomp;
my @ary = split(/\:/);
$ary[0] =~ s/\./\//g;
my $path = $ary[0] . '/' . $ary[1] . '/' . $ary[2];
system "mkdir -p output/$path";
system "cp ~/.m2/repository/$path/* output/$path";
print $path . "\n";
}
__DATA__
org.yaml:snakeyaml:1.29
junit:junit:4.13.2
org.bouncycastle:bcmail-jdk14:1.38
xalan:xalan:2.7.2
org.bouncycastle:bctsp-jdk14:1.38
xerces:xercesImpl:2.12.0
ch.qos.logback:logback-classic:1.2.10
org.bouncycastle:bcprov-jdk14:1.38
commons-collections:commons-collections:3.2.2
bouncycastle:bcprov-jdk14:138
commons-fileupload:commons-fileupload:1.4
org.jdom:jdom2:2.0.6.1
ch.qos.logback:logback-core:1.2.10
com.thoughtworks.xstream:xstream:1.4.10
bouncycastle:bcmail-jdk14:138