Skip to content

Commit

Permalink
SDK-373 - Add goal to packager plugin to facilitate transition from c…
Browse files Browse the repository at this point in the history
…onfig to content packages (#28)
  • Loading branch information
mseaton authored Dec 17, 2024
1 parent 533fdd4 commit 1bbb0a2
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.maven.plugin.BuildPluginManager;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.twdata.maven.mojoexecutor.MojoExecutor;

Expand All @@ -37,10 +38,10 @@
*/
public abstract class AbstractPackagerConfigMojo extends AbstractMojo {

@Component
@Parameter(defaultValue = "${project}", readonly = true)
private MavenProject mavenProject;

@Component
@Parameter(defaultValue = "${session}", readonly = true)
private MavenSession mavenSession;

@Component
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.maven.plugins.packager.config;

import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

import static org.twdata.maven.mojoexecutor.MojoExecutor.configuration;
import static org.twdata.maven.mojoexecutor.MojoExecutor.element;
import static org.twdata.maven.mojoexecutor.MojoExecutor.executeMojo;
import static org.twdata.maven.mojoexecutor.MojoExecutor.goal;
import static org.twdata.maven.mojoexecutor.MojoExecutor.plugin;

/**
* The purpose of this Mojo is to support the migration away from config packages to content packages
* The intent is to bundle up the same configurations as content packages
*/
@Mojo(name = "create-content-package", defaultPhase = LifecyclePhase.COMPILE)
public class CreateContentPackageMojo extends AbstractPackagerConfigMojo {

@Parameter(property = "name", defaultValue = "${project.name}")
private String name;

@Parameter(property = "version", defaultValue = "${project.version}")
private String version;

@Parameter(property = "sourceConfigurationDir", defaultValue = "${project.basedir}/configuration")
private File sourceConfigurationDir;

@Parameter(property = "sourceFrontendSubdir", defaultValue = "frontend")
private String sourceFrontendSubdir;

// Dependency configurations that this project wishes to import from other projects
@Parameter(property = "dependencyFile", defaultValue = "dependencies.yml")
private File dependenciesFile;

@Parameter(property = "targetDir", defaultValue = "${project.build.directory}/package")
private File targetDir;

/**
* @throws MojoExecutionException if an error occurs
*/
public void execute() throws MojoExecutionException {
List<String> contentProperties = new ArrayList<>();
contentProperties.add("# Content Package");
contentProperties.add("name=" + name);
contentProperties.add("version=" + version);

Properties constants = loadPropertiesFromFile(getCompiledConstantsFile());
if (constants != null && !constants.isEmpty()) {
contentProperties.add("");
contentProperties.add("# Constants");
Enumeration<Object> e = constants.keys();
while (e.hasMoreElements()) {
Object key = e.nextElement();
contentProperties.add("var." + key + "=" + constants.get(key));
}
}

File contentPropertiesFile = new File(targetDir, "content.properties");
try {
FileUtils.writeLines(contentPropertiesFile, "UTF-8", contentProperties);
}
catch (IOException e) {
throw new MojoExecutionException("Unable to write content.properties", e);
}

File targetConfigurationDir = new File(targetDir, "configuration");
File backendTargetDir = new File(targetConfigurationDir, "backend_configuration");

executeMojo(
plugin("org.apache.maven.plugins", "maven-resources-plugin", "3.1.0"),
goal("copy-resources"),
configuration(
element("outputDirectory", backendTargetDir.getAbsolutePath()),
element("encoding", "UTF-8"),
element("overwrite", "true"),
element("resources",
element("resource",
element("directory", sourceConfigurationDir.getAbsolutePath()),
element("filtering", "false"),
element("includes",
element("include", "**/*")
),
element("excludes",
element("exclude", sourceFrontendSubdir + "/**/*")
)
)
)
),
getMavenExecutionEnvironment()
);

File frontendSourceDir = new File(sourceConfigurationDir, sourceFrontendSubdir);
File frontendTargetDir = new File(targetConfigurationDir, "frontend_configuration");

executeMojo(
plugin("org.apache.maven.plugins", "maven-resources-plugin", "3.1.0"),
goal("copy-resources"),
configuration(
element("outputDirectory", frontendTargetDir.getAbsolutePath()),
element("encoding", "UTF-8"),
element("overwrite", "true"),
element("resources",
element("resource",
element("directory", frontendSourceDir.getAbsolutePath()),
element("filtering", "false"),
element("includes",
element("include", "**/*")
)
)
)
),
getMavenExecutionEnvironment()
);

}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<source>8</source>
<target>8</target>
Expand Down

0 comments on commit 1bbb0a2

Please sign in to comment.