1 package org.andromda.maven.plugin;
2
3 import java.io.File;
4 import java.net.URL;
5 import org.andromda.core.AndroMDA;
6 import org.andromda.core.common.ResourceUtils;
7 import org.andromda.core.configuration.Configuration;
8 import org.andromda.core.configuration.Model;
9 import org.andromda.core.configuration.Repository;
10 import org.apache.maven.plugin.MojoExecutionException;
11
12
13
14
15
16
17
18
19
20 public class AndroMDAMojo
21 extends AbstractAndroMDAMojo
22 {
23
24
25
26
27
28
29
30 private boolean lastModifiedCheck = true;
31
32
33
34
35
36
37
38
39 private boolean skipProcessing = false;
40
41
42
43
44
45
46 private File buildSourceDirectory;
47
48
49
50
51
52
53
54 private File modelOutputHistory;
55
56
57
58
59 public void setLastModifiedCheck(boolean lastModifiedCheck)
60 {
61 this.lastModifiedCheck = lastModifiedCheck;
62 }
63
64
65
66
67 public void setBuildSourceDirectory(File buildSourceDirectory)
68 {
69 this.buildSourceDirectory = buildSourceDirectory;
70 }
71
72
73
74
75 public void setModelOutputHistory(File modelOutputHistory)
76 {
77 this.modelOutputHistory = modelOutputHistory;
78 }
79
80
81
82
83 public void execute(final Configuration configuration)
84 throws MojoExecutionException
85 {
86 if (getLog().isDebugEnabled())
87 {
88 getLog().debug("lastModifiedCheck="+this.lastModifiedCheck + " skipProcessing="+this.skipProcessing
89 + " modelOutputHistory="+this.modelOutputHistory + " allowMultipleRuns="+this.allowMultipleRuns);
90 }
91 if (!this.skipProcessing)
92 {
93 boolean execute = true;
94 if (this.lastModifiedCheck)
95 {
96 long date = this.getLastModelConfigDate(configuration);
97 execute = ResourceUtils.modifiedAfter(date,
98 this.buildSourceDirectory);
99 if (getLog().isDebugEnabled())
100 {
101 getLog().debug("this.lastModifiedCheck="+this.lastModifiedCheck + " execute="+execute + " date="+date);
102 }
103 }
104 if (execute)
105 {
106 final AndroMDA andromda = AndroMDA.newInstance();
107 final String historyPath = this.modelOutputHistory!=null ? this.modelOutputHistory.getAbsolutePath() : null;
108 andromda.run(configuration, lastModifiedCheck, historyPath);
109 andromda.shutdown();
110 }
111 else
112 {
113 this.getLog().info("Files are up-to-date, skipping AndroMDA execution");
114 }
115 }
116 if (this.buildSourceDirectory != null)
117 {
118 this.getProject().addCompileSourceRoot(this.buildSourceDirectory.getPath());
119 }
120 }
121
122
123
124
125
126 private long getLastModelConfigDate(final Configuration configuration)
127 {
128 final URL configurationUri = ResourceUtils.toURL(this.configurationUri);
129 long date = ResourceUtils.getLastModifiedTime(configurationUri);
130 final Repository[] repositories = configuration.getRepositories();
131 int repositoryCount = repositories.length;
132 for (int ctr = 0; ctr < repositoryCount; ctr++)
133 {
134 final Repository repository = repositories[ctr];
135 if (repository != null)
136 {
137 final Model[] models = repository.getModels();
138 final int modelCount = models.length;
139 for (int ctr2 = 0; ctr2 < modelCount; ctr2++)
140 {
141 final Model model = models[ctr2];
142 long newDate = model.getLastModified();
143 if (newDate > date)
144 {
145 date = newDate;
146 }
147 }
148 }
149 }
150 return date;
151 }
152 }