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

Feature/Support Maven Profiles #179

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 @@ -19,13 +19,17 @@ class MuleLinterCli implements Runnable {
@CommandLine.Option(names = ['-f', '--format'], defaultValue = 'CONSOLE',
description = 'Report Output Format. Valid values: ${COMPLETION-CANDIDATES}')
ReportFormat outputFormat

@CommandLine.Option(names = ['-p', '--profiles'], arity = '0..*', description = 'List of Maven profiles to apply', split = ',')
List<String> profiles

static void main(String... args) {
new CommandLine(new MuleLinterCli()).execute(args)
}

@Override
void run() {
MuleLinter ml = new MuleLinter(appDir, ruleConfiguration, outputFormat)
MuleLinter ml = new MuleLinter(appDir, ruleConfiguration, outputFormat, profiles)
ml.runLinter()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class MuleLinter {
List<RuleSet> ruleSetList = []
ReportFormat outputFormat

MuleLinter(File applicationDirectory, File ruleConfigFile, ReportFormat outputFormat) {
this.app = new MuleApplication(applicationDirectory)
MuleLinter(File applicationDirectory, File ruleConfigFile, ReportFormat outputFormat, List<String> profiles = null) {
this.app = new MuleApplication(applicationDirectory, profiles)
//ruleSetList = parseConfigurationFile(ruleConfigFile)
ruleSetList = processDSL(ruleConfigFile)
this.outputFormat= outputFormat
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ class MuleApplication implements Application {
GitIgnoreFile gitignoreFile
MuleArtifact muleArtifact

MuleApplication(File applicationPath) {
MuleApplication(File applicationPath, List<String> profiles = null) {
this.applicationPath = applicationPath
if (!this.applicationPath.exists()) {
throw new FileNotFoundException( APPLICATION_DOES_NOT_EXIST + applicationPath.absolutePath)
}
File pFile = new File(applicationPath, POM_FILE)
// if pom.xml exists in application, get the effective-pom.xml for the application.
if (pFile.exists())
pFile = getEffectivePomFile(pFile)
pFile = getEffectivePomFile(pFile, profiles)
pomFile = new PomFile(pFile, pFile.exists() ? new MuleXmlParser().parse(pFile) : null)
gitignoreFile = new GitIgnoreFile(applicationPath, GITIGNORE_FILE)
readmeFile = new ReadmeFile(applicationPath, README)
Expand All @@ -57,7 +57,7 @@ class MuleApplication implements Application {
* 2. Set MAVEN_HOME environment variable in the system executing mule-linter.
* returns File
*/
File getEffectivePomFile(File pFile){
File getEffectivePomFile(File pFile, List<String> profiles = null){
def mavenHome = null
// Update mavenHome from system property - maven.home
if (System.getProperty('maven.home') != null)
Expand All @@ -74,6 +74,7 @@ class MuleApplication implements Application {
setGoals([mvnGoals])
setPomFile(pFile)
setShowErrors(true)
setProfiles(profiles)
it
}
def mavenInvoker = new DefaultInvoker()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,24 @@ class PomDependencyVersionRuleTest extends Specification {
violations.size() == 0
}

def 'Correct Version With Maven Profile'() {
given:
testApp.addFile(PomFile.POM_XML, DEPENDENCY_PROFILE_PROPERTY_VERSION_POM);
Rule rule = new PomDependencyVersionRule()
rule.groupId = 'org.mule.connectors'
rule.artifactId = 'mule-http-connector'
rule.artifactVersion = '2.9.5'
rule.versionOperator = 'EQUAL'
rule.init()

when:
app = new MuleApplication(testApp.appDir, ['testProfile'])
List<RuleViolation> violations = rule.execute(app)

then:
violations.size() == 0
}

private static final String MISSING_DEPENDENCY_POM = '''<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
Expand Down Expand Up @@ -258,5 +276,61 @@ class PomDependencyVersionRuleTest extends Specification {
</pluginRepository>
</pluginRepositories>
</project>
'''

private static final String DEPENDENCY_PROFILE_PROPERTY_VERSION_POM = '''<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.avioconsulting.mulelinter</groupId>
<artifactId>sample-mule-app</artifactId>
<version>1.0.0</version>
<packaging>mule-application</packaging>
<name>sample-mule-app-sys-api</name>
<profiles>
<profile>
<id>testProfile</id>
<properties>
<http.connector.version>2.9.5</http.connector.version>
</properties>
</profile>
</profiles>
<properties>
<app.runtime>4.2.1</app.runtime>
<mule.maven.plugin.version>3.3.5</mule.maven.plugin.version>
<http.connector.version>1.3.2</http.connector.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.mule.tools.maven</groupId>
<artifactId>mule-maven-plugin</artifactId>
<version>${mule.maven.plugin.version}</version>
<extensions>true</extensions>
<configuration>
<classifier>mule-application</classifier>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.mule.connectors</groupId>
<artifactId>mule-http-connector</artifactId>
<version>${http.connector.version}</version>
<classifier>mule-plugin</classifier>
</dependency>
</dependencies>
<pluginRepositories>
<pluginRepository>
<id>mulesoft-releases</id>
<name>mulesoft release repository</name>
<layout>default</layout>
<url>https://repository.mulesoft.org/releases/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
'''
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.io.File;
import java.util.List;
import java.util.stream.Collectors;

public abstract class AbstractMuleLinterMojo extends AbstractMojo {

Expand All @@ -30,6 +31,13 @@ public String getProjectVersion (){
return this.project.getVersion();
}

public List<String> getProjectInjectedProfileIds (){
return this.project.getInjectedProfileIds().values().stream()
.flatMap(List::stream)
.distinct()
.collect(Collectors.toList());
}

public void failIfNeeded(boolean shouldFail, List<RuleViolation> violations){
if(shouldFail && !violations.isEmpty()
&& violations.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public class MuleLinterValidateMojo extends AbstractMuleLinterMojo {

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
MuleLinter muleLinter = new MuleLinter(appDir, ruleConfiguration, ReportFormat.valueOf(FormatOptionsEnum.CONSOLE.name().toUpperCase()));
MuleLinter muleLinter = new MuleLinter(appDir, ruleConfiguration, ReportFormat.valueOf(FormatOptionsEnum.CONSOLE.name().toUpperCase()),
this.getProjectInjectedProfileIds());
this.getLog().debug(format("Executing linter config %s against application %s", ruleConfiguration.getAbsolutePath(), appDir.getAbsolutePath()));
RuleExecutor ruleExecutor = muleLinter.buildLinterExecutor();
try {
Expand Down
Loading