Maven polyglot

This commit is contained in:
eelhazati
2018-10-02 21:35:45 +01:00
parent d7dac62bfb
commit 5f2b7d5fef
10 changed files with 410 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.maven.polyglot</groupId>
<artifactId>maven-polyglot-json-extension</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>3.5.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-component-metadata</artifactId>
<version>1.7.1</version>
<executions>
<execution>
<goals>
<goal>generate-metadata</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,62 @@
package com.demo.polyglot;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.maven.model.Model;
import org.apache.maven.model.building.FileModelSource;
import org.apache.maven.model.building.ModelProcessor;
import org.apache.maven.model.io.ModelParseException;
import org.apache.maven.model.io.ModelReader;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.util.ReaderFactory;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.Map;
@Component(role = ModelProcessor.class)
public class CustomModelProcessor implements ModelProcessor {
private static final String XML_POM = "pom.xml";
private static final String JSON_POM = "pom.json";
private static final String JSON_EXT = ".json";
ObjectMapper objectMapper = new ObjectMapper();
@Requirement
private ModelReader modelReader;
@Override
public File locatePom(File projectDirectory) {
File pomFile = new File(projectDirectory, JSON_POM);
if (!pomFile.exists()) {
pomFile = new File(projectDirectory, XML_POM);
}
return pomFile;
}
@Override
public Model read(InputStream input, Map<String, ?> options) throws IOException, ModelParseException {
try (final Reader in = ReaderFactory.newPlatformReader(input)) {
return read(in, options);
}
}
@Override
public Model read(Reader reader, Map<String, ?> options) throws IOException, ModelParseException {
FileModelSource source = (options != null) ? (FileModelSource) options.get(SOURCE) : null;
if (source != null && source.getLocation().endsWith(JSON_EXT)) {
Model model = objectMapper.readValue(reader, Model.class);
return model;
}
//It's a normal maven project with a pom.xml file
return modelReader.read(reader, options);
}
@Override
public Model read(File input, Map<String, ?> options) throws IOException, ModelParseException {
return null;
}
}