-
Notifications
You must be signed in to change notification settings - Fork 0
/
nbMavenize.sh
252 lines (206 loc) · 9.71 KB
/
nbMavenize.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/bin/bash
# Jerome Lelasseux 2023 - Tested on Win10/cygwin
# =============================================================================================
function usage()
{
echo "USAGE: nbMavenize [ant_module_dir] [maven_parent_project_dir]"
echo "Imports an Ant-based module to a Maven-based project for a Netbeans platform application."
echo
echo "EXAMPLE: nbMavenize antNbApp/XmlParser mavenNbApp"
echo
echo "Imported module is stored in the 'modules' subdirectory of the maven parent project."
echo "The source and resources files are copied and the module pom.xml is created."
echo "The pom.xml files of the parent and application projects are updated (if needed)."
echo
echo "The script does not handle all possible cases. Search for 'MANUAL ACTION NEEDED' in the script"
echo "output for examples of post-script required actions."
exit
}
# The template pom.xml for a NB platform app module. Taken from the pom.xml created by the NB 17 wizard.
# $1 parent groupId
# $2 parent artifactId
# $3 parent version
# $4 module artifactId
# $5 module name
function echoBaseModulePom()
{
echo "
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>$1</groupId>
<artifactId>$2</artifactId>
<version>$3</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<artifactId>$4</artifactId>
<name>$5</name>
<packaging>nbm</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.netbeans.utilities</groupId>
<artifactId>nbm-maven-plugin</artifactId>
<configuration>
<publicPackages>
</publicPackages>
</configuration>
<extensions>true</extensions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestFile>\${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
"
}
# ===================================================================================================
# MAIN
# ===================================================================================================
if [[ ! $# -eq 2 ]]
then
usage
fi
srcDir="$1"
if [[ ! -e "$srcDir/manifest.mf" ]] || [[ ! -d "$srcDir/nbproject" ]] || [[ ! -d "$srcDir/src" ]]
then
echo "Missing files in source Netbeans module directory $srcDir"
exit
fi
moduleName=$(basename $srcDir)
moduleCodeNameDot=$(grep "^[[:blank:]]*OpenIDE-Module *:" $srcDir/manifest.mf | sed -e 's/^ *OpenIDE-Module *: *//' | tr -d '\r') # get rid of annoying trailing \n
moduleCodeNameDash=$(echo $moduleCodeNameDot | tr . -)
moduleCodeNameSlash=$(echo $moduleCodeNameDot | tr . /)
moduleNameLo=${moduleName,,} # lower case
destDir="$2"
targetFile="$destDir/pom.xml"
if [[ ! -e "$targetFile" ]]
then
echo "Missing file $targetFile"
exit
fi
destModuleDir="$destDir/modules/$moduleName"
destModuleDirAbs=$(realpath $destModuleDir)
parentGroupId=$(grep -m 1 "^[[:blank:]]*<groupId>" $targetFile | sed -E 's/ *<\/?groupId> *//g' | tr -d '\r')
parentArtifactId=$(grep -m 1 "^[[:blank:]]*<artifactId>" $targetFile | sed -E 's/ *<\/?artifactId> *//g' | tr -d '\r')
parentVersion=$(grep -m 1 "^[[:blank:]]*<version>" $targetFile | sed -E 's/ *<\/?version> *//g' | tr -d '\r')
echo -e "\n\n=========================================================================================="
echo "=========================================================================================="
echo "IMPORTING moduleName=$moduleName moduleCodeNameDot=$moduleCodeNameDot destModuleDir=$destModuleDir"
echo "=========================================================================================="
echo "=========================================================================================="
if [[ -d "$destModuleDir" ]]
then
echo "$destModuleDir already exists"
exit
fi
targetFile="$destModuleDir/pom.xml"
echo -e "\nInitializing $destModuleDir and $targetFile ==============="
set -o xtrace
mkdir -p $destModuleDir/src/main/java
mkdir -p $destModuleDir/src/main/nbm
mkdir -p $destModuleDir/src/main/resources
set +o xtrace
echo "Creating $targetFile..."
echoBaseModulePom $parentGroupId $parentArtifactId $parentVersion $moduleCodeNameDash $moduleName > $targetFile
srcFile="$srcDir/manifest.mf"
targetFile="$destModuleDir/src/main/nbm/manifest.mf"
echo -e "\nCopying and adapting $targetFile =============="
set -o xtrace
cat $srcFile | grep -Ev "^[[:blank:]]*(OpenIDE-Module-Specification-Version|OpenIDE-Module)[[:blank:]]*:" > $targetFile
cat $targetFile
set +o xtrace
echo -e "\nCopying source files (.java .form .pdf .txt) ===================="
set -o xtrace
cd $srcDir/src
find . -type f \( -iname '*.java' -o -iname '*.form' -o -iname '*.pdf' -o -iname '*.txt' \) | xargs cp -v --parents -t $destModuleDirAbs/src/main/java
cd -
set +o xtrace
echo -e "\nCopying resources files ====================="
set -o xtrace
cd $srcDir/src
find . -type f -not \( -iname '*.java' -o -iname '*.form' -o -iname '*.pdf' -o -iname '*.txt' \) | xargs cp -v --parents -t $destModuleDirAbs/src/main/resources
cd -
set +o xtrace
targetFile="$destDir/pom.xml"
echo -e "\nAdding module $moduleName to parent $targetFile ====================="
if ! grep -qi ">modules/$moduleName<" $targetFile ; then
set -o xtrace
sed -i "s/<\/modules>/<module>modules\/$moduleName<\/module><\/modules>/" $targetFile
set +o xtrace
else
echo " > pom.xml was already updated"
fi
targetFile="$destDir/application/pom.xml"
echo -e "\nAdding dependency on $moduleCodeNameDash in application $targetFile ====================="
if ! grep -qi "artifactId>$moduleCodeNameDash<" $targetFile; then
set -o xtrace
sed -i "s/<\/dependencies>/ <dependency> <groupId>\$\{project\.groupId\}<\/groupId> <artifactId>$moduleCodeNameDash<\/artifactId> <version>\$\{project\.version\}<\/version> <\/dependency> <\/dependencies>/" $targetFile
set +o xtrace
else
echo " > pom.xml was already updated"
fi
srcProjectXml="$srcDir/nbproject/project.xml"
targetFile="$destModuleDir/pom.xml"
echo -e "\nAdding public packages in module $targetFile ====================="
set +o xtrace
# Get source public packages
pkgs=$(grep "<package>" $srcProjectXml | sed -E 's/[[:blank:]]*<\/?package>[[:blank:]]*//g')
for pkg in $pkgs; do
pkg=$(echo $pkg | tr -d '\r') # get rid of annoying ending CR
if ! grep -qi "publicPackage>$pkg<" $targetFile; then
echo " > adding public package $pkg"
sed -i "s/<\/publicPackages>/ <publicPackage>$pkg<\/publicPackage>\n<\/publicPackages>/" $targetFile
fi
done
targetFile="$destModuleDir/pom.xml"
echo -e "\nAdding non-Netbeans platform dependencies in module $targetFile ====================="
# Note that project.xml contains also a code-name-base for the module itself, so we need to remove it too
pkgs=$(grep '<code-name-base>' $srcProjectXml | grep -v -E "org.(netbeans|openide)" | grep -v ">$moduleCodeNameDot<" \
| sed -E 's/[[:blank:]]*<\/?code-name-base>[[:blank:]]*//g')
for pkg in $pkgs; do
pkg=$(echo $pkg | tr . - | tr -d '\r') # get rid of annoying ending CR
if ! grep -qi "<artifactId>$pkg<" $targetFile; then
echo " > adding dependency to $pkg"
sed -i "s/<\/dependencies>/ <dependency> <groupId>\$\{project\.groupId\}<\/groupId> <artifactId>$pkg<\/artifactId> <version>\$\{project\.version\}<\/version> <\/dependency> <\/dependencies>/" $targetFile
fi
done
targetFile="$destModuleDir/pom.xml"
echo -e "\nAdding Netbeans platform dependencies in module $targetFile (excluding unit test dependencies) ====================="
pkgs=$(grep -E "<code-name-base>org.(netbeans|openide)" $srcProjectXml | grep -v "junit" | sed -E 's/[[:blank:]]*<\/?code-name-base>[[:blank:]]*//g')
for pkg in $pkgs; do
pkg=$(echo $pkg | tr . - | tr -d '\r') # get rid of annoying ending CR
if ! grep -qi "<artifactId>$pkg<" $targetFile; then
echo " > adding dependency to $pkg"
sed -i "s/<\/dependencies>/ <dependency> <groupId>org.netbeans.api<\/groupId> <artifactId>$pkg<\/artifactId> <version>\$\{netbeans\.version\}<\/version> <\/dependency> <\/dependencies>/" $targetFile
fi
done
echo -e "\nChecking for post-script manual actions required ====================="
if grep -q '^[[:blank:]]*<implementation-version/>' $srcProjectXml;
then
echo "## MANUAL ACTION NEEDED: There are one or more implementation-version dependencies in $srcProjectXml. You will need to manually update $targetFile to configure the nbm-maven-plugin accordingly: check the online doc for goal=manifest, parameter=moduleDependencies, type=impl"
fi
doReleaseDirCheck=true
grep -q '^[[:blank:]]*<binary-origin' $srcProjectXml
if [[ $? -eq 0 ]]
then
echo "## MANUAL ACTION NEEDED: There are one or more external libraries specified in $srcProjectXml. You will need to manually add the corresponding dependencies in $targetFile"
doReleaseDirCheck=false
fi
if $doReleaseDirCheck && [[ -d $srcDir/release ]]
then
echo "## MANUAL ACTION NEEDED: There is a release subdirectory in $srcProjectXml. You will need to manually copy the release files and update $targetFile to configure the nbm-maven-plugin accordingly: check the online doc for goal=nbm, parameter=nbmResources"
fi