Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDK-373 - Add goal to packager plugin to facilitate transition from c… #28

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Config packages never lived outside of PIH, did they, or am I forgetting? Might be worth referencing these as "PIH" config packages in the comments to help people 10 years from now remember what they were (or just realize they can ignore them)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right, the packager plugin... maybe just reference that in the comments?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duh, sorry for all the spam... I was thinking this change was in the SDK... of course you don't need to reference the packager plugin since this is the package plugin... anyway, LGTM, ignore my comments...

*/
@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
Loading