20
20
21
21
public class BasicRest extends ShamrockTemplate {
22
22
private Map <String , Object > context ;
23
- private String className = "ShamrockResource" ;
23
+ private String className ;
24
24
private String path = "/hello" ;
25
25
private File projectRoot ;
26
26
private File srcMain ;
@@ -39,37 +39,43 @@ public void generate(final File projectRoot, Map<String, Object> parameters) thr
39
39
initProject ();
40
40
setupContext ();
41
41
42
- createClasses ();
42
+ if (className != null ) {
43
+ createClasses ();
44
+ }
43
45
createIndexPage ();
44
46
createDockerFile ();
45
47
createMicroProfileConfig ();
46
48
}
47
49
48
50
private void setupContext () {
49
51
MojoUtils .getAllProperties ().forEach ((k , v ) -> context .put (k .replace ("-" , "_" ), v ));
50
- String packageName = (String ) context .get (PACKAGE_NAME );
51
- if (className .endsWith (MojoUtils .JAVA_EXTENSION )) {
52
- className = className .substring (0 , className .length () - MojoUtils .JAVA_EXTENSION .length ());
53
- }
54
52
55
- if (className .contains ("." )) {
56
- int idx = className .lastIndexOf ('.' );
57
- packageName = className .substring (0 , idx );
58
- className = className .substring (idx + 1 );
59
- }
53
+ if (className != null ) {
54
+ String packageName = (String ) context .get (PACKAGE_NAME );
60
55
61
- if (packageName != null ) {
62
- File packageDir = new File (srcMain , packageName .replace ('.' , '/' ));
63
- File testPackageDir = new File (testMain , packageName .replace ('.' , '/' ));
64
- srcMain = mkdirs (packageDir );
65
- testMain = mkdirs (testPackageDir );
66
- }
56
+ if (className .endsWith (MojoUtils .JAVA_EXTENSION )) {
57
+ className = className .substring (0 , className .length () - MojoUtils .JAVA_EXTENSION .length ());
58
+ }
67
59
68
- context .put (CLASS_NAME , className );
69
- context .put (RESOURCE_PATH , path );
60
+ if (className .contains ("." )) {
61
+ int idx = className .lastIndexOf ('.' );
62
+ packageName = className .substring (0 , idx );
63
+ className = className .substring (idx + 1 );
64
+ }
65
+
66
+ if (packageName != null ) {
67
+ File packageDir = new File (srcMain , packageName .replace ('.' , '/' ));
68
+ File testPackageDir = new File (testMain , packageName .replace ('.' , '/' ));
69
+ srcMain = mkdirs (packageDir );
70
+ testMain = mkdirs (testPackageDir );
71
+ }
70
72
71
- if (packageName != null ) {
72
- context .put (PACKAGE_NAME , packageName );
73
+ context .put (CLASS_NAME , className );
74
+ context .put (RESOURCE_PATH , path );
75
+
76
+ if (packageName != null ) {
77
+ context .put (PACKAGE_NAME , packageName );
78
+ }
73
79
}
74
80
}
75
81
@@ -96,9 +102,8 @@ private boolean initProject() throws IOException {
96
102
context .put (PROJECT_ARTIFACT_ID , model .getArtifactId ());
97
103
}
98
104
99
- className = get ("className" ,
100
- format ("%s.%s.%s" , context .get (PROJECT_GROUP_ID ), context .get (PROJECT_ARTIFACT_ID ),
101
- "MyResource" ));
105
+ // If className is null we disable the generation of the Jax-RS resource.
106
+ className = get ("className" , null );
102
107
path = get (RESOURCE_PATH , path );
103
108
104
109
srcMain = mkdirs (new File (projectRoot , "src/main/java" ));
@@ -115,7 +120,9 @@ private void generate(final String templateName, final Map<String, Object> conte
115
120
final BufferedReader stream = new BufferedReader (new InputStreamReader (getClass ().getResourceAsStream (path )))) {
116
121
String template = stream .lines ().collect (Collectors .joining ("\n " ));
117
122
for (Entry <String , Object > e : context .entrySet ()) {
118
- template = template .replace (format ("${%s}" , e .getKey ()), e .getValue ().toString ());
123
+ if (e .getValue () != null ) { // Exclude null values (classname and path can be null)
124
+ template = template .replace (format ("${%s}" , e .getKey ()), e .getValue ().toString ());
125
+ }
119
126
}
120
127
out .write (template );
121
128
}
0 commit comments