Skip to content

Commit

Permalink
added maven goals to build for specific targets without having to pro…
Browse files Browse the repository at this point in the history
…vide any profiles or properties

This will make it easier to inspect the build targets that are available via the Maven side-bar.
Goals include:
build-android
build-android-gradle-project
build-jar
build-ios
build-ios-release
build-ios-xcode-project
build-javascript
build-mac-desktop
build-windows-desktop
build-windows-uwp

This also includes a goal to install the intelliJ run profiles into the project, in case they were lost.
  • Loading branch information
shannah committed Dec 31, 2023
1 parent e156f18 commit a86ec00
Show file tree
Hide file tree
Showing 13 changed files with 1,151 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.codename1.maven.buildWrappers;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.invoker.*;

import java.io.File;
import java.util.Collections;
import java.util.Properties;

/**
* Base class for build wrapper Mojos.
*
* Wrapper mojos were created to make it easier to build Codename One targets
* directly in IDEs like IntelliJ without having to provide any properties or profiles.
*
* The user will see "buildAndroid", "buildIos" and "buildIosXcodeProject" goals, etc..
* listed in the Maven sidebar, and they can just double-click them to invoke them
* directly.
*/
public abstract class AbstractBuildWrapperMojo extends AbstractMojo {

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

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (!project.isExecutionRoot()) {
getLog().info("Skipping execution for non-root project");
return;
}
InvocationRequest request = new DefaultInvocationRequest();
request.setPomFile(new File("pom.xml"));
request.setGoals(Collections.singletonList("package"));

Properties properties = new Properties();
properties.setProperty("skipTests", "true");
properties.setProperty("codename1.platform", getPlatform());
if (getBuildTarget() != null) {
properties.setProperty("codename1.buildTarget", getBuildTarget());
}
request.setProperties(properties);

if (buildExecutableJar()) {
request.setProfiles(Collections.singletonList("executable-jar"));
}

Invoker invoker = new DefaultInvoker();

try {
InvocationResult result = invoker.execute(request);
if (result.getExitCode() != 0) {
throw new MojoFailureException("Failed to build project with exit code " + result.getExitCode());
}
} catch (MavenInvocationException e) {
throw new MojoExecutionException("Failed to invoke Maven", e);
}
}

protected abstract String getPlatform();
protected abstract String getBuildTarget();

protected boolean buildExecutableJar() {
return false;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.codename1.maven.buildWrappers;


import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.ResolutionScope;

@Mojo(name="buildAndroidGradleProject", requiresDependencyResolution = ResolutionScope.NONE,
requiresDependencyCollection = ResolutionScope.NONE)
public class BuildAndroidGradleProjectMojo extends AbstractBuildWrapperMojo {
@Override
protected String getPlatform() {
return "android";
}

@Override
protected String getBuildTarget() {
return "android-source";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.codename1.maven.buildWrappers;


import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.ResolutionScope;

@Mojo(name="buildAndroid", requiresDependencyResolution = ResolutionScope.NONE,
requiresDependencyCollection = ResolutionScope.NONE)
public class BuildAndroidMojo extends AbstractBuildWrapperMojo {
@Override
protected String getPlatform() {
return "android";
}

@Override
protected String getBuildTarget() {
return "android-device";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.codename1.maven.buildWrappers;


import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.ResolutionScope;

@Mojo(name="buildExecutableJar", requiresDependencyResolution = ResolutionScope.NONE,
requiresDependencyCollection = ResolutionScope.NONE)
public class BuildExecutableJarMojo extends AbstractBuildWrapperMojo {
@Override
protected String getPlatform() {
return "javase";
}

@Override
protected String getBuildTarget() {
return null;
}

@Override
protected boolean buildExecutableJar() {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.codename1.maven.buildWrappers;


import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.ResolutionScope;

@Mojo(name="buildIos", requiresDependencyResolution = ResolutionScope.NONE,
requiresDependencyCollection = ResolutionScope.NONE)
public class BuildIosMojo extends AbstractBuildWrapperMojo {
@Override
protected String getPlatform() {
return "ios";
}

@Override
protected String getBuildTarget() {
return "ios-device";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.codename1.maven.buildWrappers;


import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.ResolutionScope;

@Mojo(name="buildIosRelease", requiresDependencyResolution = ResolutionScope.NONE,
requiresDependencyCollection = ResolutionScope.NONE)
public class BuildIosReleaseMojo extends AbstractBuildWrapperMojo {
@Override
protected String getPlatform() {
return "ios";
}

@Override
protected String getBuildTarget() {
return "ios-device-release";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.codename1.maven.buildWrappers;


import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.ResolutionScope;

@Mojo(name="buildIosXcodeProject", requiresDependencyResolution = ResolutionScope.NONE,
requiresDependencyCollection = ResolutionScope.NONE)
public class BuildIosXcodeProjectMojo extends AbstractBuildWrapperMojo {
@Override
protected String getPlatform() {
return "ios";
}

@Override
protected String getBuildTarget() {
return "ios-source";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.codename1.maven.buildWrappers;


import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.ResolutionScope;

@Mojo(name="buildJavascript", requiresDependencyResolution = ResolutionScope.NONE,
requiresDependencyCollection = ResolutionScope.NONE)
public class BuildJavascriptMojo extends AbstractBuildWrapperMojo {
@Override
protected String getPlatform() {
return "javascript";
}

@Override
protected String getBuildTarget() {
return "javascript";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.codename1.maven.buildWrappers;


import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.ResolutionScope;

@Mojo(name="buildMacDesktop", requiresDependencyResolution = ResolutionScope.NONE,
requiresDependencyCollection = ResolutionScope.NONE)
public class BuildMacDesktopMojo extends AbstractBuildWrapperMojo {
@Override
protected String getPlatform() {
return "javase";
}

@Override
protected String getBuildTarget() {
return "mac-os-x-desktop";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.codename1.maven.buildWrappers;


import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.ResolutionScope;

@Mojo(name="buildWindowsDesktop", requiresDependencyResolution = ResolutionScope.NONE,
requiresDependencyCollection = ResolutionScope.NONE)
public class BuildWindowsDesktopMojo extends AbstractBuildWrapperMojo {
@Override
protected String getPlatform() {
return "javase";
}

@Override
protected String getBuildTarget() {
return "windows-desktop";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.codename1.maven.buildWrappers;


import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.ResolutionScope;

@Mojo(name="buildWindowsUWP", requiresDependencyResolution = ResolutionScope.NONE,
requiresDependencyCollection = ResolutionScope.NONE)
public class BuildWindowsDeviceMojo extends AbstractBuildWrapperMojo {
@Override
protected String getPlatform() {
return "win";
}

@Override
protected String getBuildTarget() {
return "windows-device";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.codename1.maven.intellij;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.codehaus.plexus.util.FileUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
* Goal to install the IntelliJ run profiles for the project.
*/
@Mojo(name="configure-intellij", requiresDependencyResolution = ResolutionScope.NONE,
requiresDependencyCollection = ResolutionScope.NONE)
public class InstallIntelliJRunProfilesMojo extends AbstractMojo {
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
File workspaceFile = new File(".idea/workspace.xml");
if (!workspaceFile.exists()) {
throw new MojoFailureException("No workspace.xml file found. Please run this goal from the root of your IntelliJ project.");
}

InputStream in = getClass().getResourceAsStream("workspace.xml");
if (in == null) {
throw new MojoFailureException("Failed to find workspace.xml template in resources");
}
try {
FileUtils.copyURLToFile(getClass().getResource("workspace.xml"), workspaceFile);
} catch (IOException ex) {
throw new MojoFailureException("Failed to copy workspace.xml template to " + workspaceFile.getAbsolutePath(), ex);
}
}
}
Loading

0 comments on commit a86ec00

Please sign in to comment.