Skip to content

Commit 2609d8b

Browse files
authored
Merge pull request eugenp#5381 from eelhazati/master
BAEL-1999 : Maven polyglot
2 parents 450709d + 5f2b7d5 commit 2609d8b

10 files changed

Lines changed: 410 additions & 0 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<extensions>
3+
<extension>
4+
<groupId>com.baeldung.maven.polyglot</groupId>
5+
<artifactId>maven-polyglot-json-extension</artifactId>
6+
<version>1.0-SNAPSHOT</version>
7+
</extension>
8+
</extensions>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"modelVersion": "4.0.0",
3+
"groupId": "com.baeldung.maven.polyglot",
4+
"artifactId": "maven-polyglot-json-app",
5+
"version": "1.0-SNAPSHOT",
6+
"name": "Json Maven Polyglot",
7+
"parent": {
8+
"groupId": "org.springframework.boot",
9+
"artifactId": "spring-boot-starter-parent",
10+
"version": "2.0.5.RELEASE",
11+
"relativePath": null
12+
},
13+
"properties": {
14+
"project.build.sourceEncoding": "UTF-8",
15+
"project.reporting.outputEncoding": "UTF-8",
16+
"maven.compiler.source": "1.8",
17+
"maven.compiler.target": "1.8",
18+
"java.version": "1.8"
19+
},
20+
"dependencies": [
21+
{
22+
"groupId": "org.springframework.boot",
23+
"artifactId": "spring-boot-starter-web"
24+
}
25+
],
26+
"build": {
27+
"plugins": [
28+
{
29+
"groupId": "org.springframework.boot",
30+
"artifactId": "spring-boot-maven-plugin"
31+
}
32+
]
33+
}
34+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.maven.polyglot;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.web.bind.annotation.RestController;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
8+
@RestController
9+
@SpringBootApplication
10+
public class MavenPolyglotApplication {
11+
public static void main(String[] args) {
12+
SpringApplication.run(MavenPolyglotApplication.class, args);
13+
}
14+
15+
@GetMapping("/")
16+
public String home(){
17+
return "Hello JSON Maven Model !";
18+
}
19+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
{
2+
"modules": [],
3+
"distributionManagement": null,
4+
"properties": {
5+
"project.reporting.outputEncoding": "UTF-8",
6+
"java.version": "1.8",
7+
"maven.compiler.source": "1.8",
8+
"project.build.sourceEncoding": "UTF-8",
9+
"maven.compiler.target": "1.8"
10+
},
11+
"dependencyManagement": null,
12+
"dependencies": [
13+
{
14+
"groupId": "org.springframework.boot",
15+
"artifactId": "spring-boot-starter-web",
16+
"version": null,
17+
"type": "jar",
18+
"classifier": null,
19+
"scope": null,
20+
"systemPath": null,
21+
"exclusions": [],
22+
"optional": null,
23+
"managementKey": "org.springframework.boot:spring-boot-starter-web:jar"
24+
}
25+
],
26+
"repositories": [],
27+
"pluginRepositories": [],
28+
"reports": null,
29+
"reporting": null,
30+
"modelVersion": "4.0.0",
31+
"parent": {
32+
"groupId": "org.springframework.boot",
33+
"artifactId": "spring-boot-starter-parent",
34+
"version": "2.0.5.RELEASE",
35+
"relativePath": "",
36+
"id": "org.springframework.boot:spring-boot-starter-parent:pom:2.0.5.RELEASE"
37+
},
38+
"groupId": "com.demo.polyglot",
39+
"artifactId": "maven-polyglot-app",
40+
"version": "1.0.1",
41+
"packaging": "jar",
42+
"name": "Json Maven Polyglot",
43+
"description": null,
44+
"url": null,
45+
"inceptionYear": null,
46+
"organization": null,
47+
"licenses": [],
48+
"developers": [],
49+
"contributors": [],
50+
"mailingLists": [],
51+
"prerequisites": null,
52+
"scm": null,
53+
"issueManagement": null,
54+
"ciManagement": null,
55+
"build": {
56+
"plugins": [
57+
{
58+
"inherited": null,
59+
"configuration": null,
60+
"inheritanceApplied": true,
61+
"groupId": "org.liquibase",
62+
"artifactId": "liquibase-maven-plugin",
63+
"version": "3.0.5",
64+
"extensions": null,
65+
"executions": [],
66+
"dependencies": [],
67+
"goals": null,
68+
"key": "org.liquibase:liquibase-maven-plugin",
69+
"id": "org.liquibase:liquibase-maven-plugin:3.0.5",
70+
"executionsAsMap": {}
71+
}
72+
],
73+
"pluginManagement": null,
74+
"defaultGoal": null,
75+
"resources": [],
76+
"testResources": [],
77+
"directory": null,
78+
"finalName": null,
79+
"filters": [],
80+
"sourceDirectory": null,
81+
"scriptSourceDirectory": null,
82+
"testSourceDirectory": null,
83+
"outputDirectory": null,
84+
"testOutputDirectory": null,
85+
"extensions": [],
86+
"pluginsAsMap": {
87+
"org.liquibase:liquibase-maven-plugin": {
88+
"inherited": null,
89+
"configuration": null,
90+
"inheritanceApplied": true,
91+
"groupId": "org.liquibase",
92+
"artifactId": "liquibase-maven-plugin",
93+
"version": "3.0.5",
94+
"extensions": null,
95+
"executions": [],
96+
"dependencies": [],
97+
"goals": null,
98+
"key": "org.liquibase:liquibase-maven-plugin",
99+
"id": "org.liquibase:liquibase-maven-plugin:3.0.5",
100+
"executionsAsMap": {}
101+
}
102+
}
103+
},
104+
"profiles": [],
105+
"modelEncoding": "UTF-8",
106+
"pomFile": null,
107+
"id": "com.demo.polyglot:maven-polyglot-app:jar:1.0.1",
108+
"projectDirectory": null
109+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.baeldung.maven.polyglot</groupId>
8+
<artifactId>maven-polyglot-json-extension</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>1.8</maven.compiler.source>
13+
<maven.compiler.target>1.8</maven.compiler.target>
14+
</properties>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.apache.maven</groupId>
19+
<artifactId>maven-core</artifactId>
20+
<version>3.5.4</version>
21+
<scope>provided</scope>
22+
</dependency>
23+
<dependency>
24+
<groupId>com.fasterxml.jackson.core</groupId>
25+
<artifactId>jackson-databind</artifactId>
26+
<version>2.9.6</version>
27+
</dependency>
28+
</dependencies>
29+
30+
<build>
31+
<plugins>
32+
<plugin>
33+
<groupId>org.codehaus.plexus</groupId>
34+
<artifactId>plexus-component-metadata</artifactId>
35+
<version>1.7.1</version>
36+
<executions>
37+
<execution>
38+
<goals>
39+
<goal>generate-metadata</goal>
40+
</goals>
41+
</execution>
42+
</executions>
43+
</plugin>
44+
</plugins>
45+
</build>
46+
47+
</project>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.demo.polyglot;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import org.apache.maven.model.Model;
5+
import org.apache.maven.model.building.FileModelSource;
6+
import org.apache.maven.model.building.ModelProcessor;
7+
import org.apache.maven.model.io.ModelParseException;
8+
import org.apache.maven.model.io.ModelReader;
9+
import org.codehaus.plexus.component.annotations.Component;
10+
import org.codehaus.plexus.component.annotations.Requirement;
11+
import org.codehaus.plexus.util.ReaderFactory;
12+
13+
import java.io.File;
14+
import java.io.IOException;
15+
import java.io.InputStream;
16+
import java.io.Reader;
17+
import java.util.Map;
18+
19+
@Component(role = ModelProcessor.class)
20+
public class CustomModelProcessor implements ModelProcessor {
21+
22+
private static final String XML_POM = "pom.xml";
23+
private static final String JSON_POM = "pom.json";
24+
private static final String JSON_EXT = ".json";
25+
26+
ObjectMapper objectMapper = new ObjectMapper();
27+
28+
@Requirement
29+
private ModelReader modelReader;
30+
31+
@Override
32+
public File locatePom(File projectDirectory) {
33+
File pomFile = new File(projectDirectory, JSON_POM);
34+
if (!pomFile.exists()) {
35+
pomFile = new File(projectDirectory, XML_POM);
36+
}
37+
return pomFile;
38+
}
39+
40+
@Override
41+
public Model read(InputStream input, Map<String, ?> options) throws IOException, ModelParseException {
42+
try (final Reader in = ReaderFactory.newPlatformReader(input)) {
43+
return read(in, options);
44+
}
45+
}
46+
47+
@Override
48+
public Model read(Reader reader, Map<String, ?> options) throws IOException, ModelParseException {
49+
FileModelSource source = (options != null) ? (FileModelSource) options.get(SOURCE) : null;
50+
if (source != null && source.getLocation().endsWith(JSON_EXT)) {
51+
Model model = objectMapper.readValue(reader, Model.class);
52+
return model;
53+
}
54+
//It's a normal maven project with a pom.xml file
55+
return modelReader.read(reader, options);
56+
}
57+
58+
@Override
59+
public Model read(File input, Map<String, ?> options) throws IOException, ModelParseException {
60+
return null;
61+
}
62+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<extensions>
3+
<extension>
4+
<groupId>io.takari.polyglot</groupId>
5+
<artifactId>polyglot-yaml</artifactId>
6+
<version>0.3.1</version>
7+
</extension>
8+
</extensions>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
modelVersion: 4.0.0
2+
groupId: com.baeldung.maven.polyglot
3+
artifactId: maven-polyglot-yml-app
4+
version: 1.0-SNAPSHOT
5+
name: 'YAML Demo'
6+
7+
properties: {maven.compiler.source: 1.8, maven.compiler.target: 1.8}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.maven.polyglot;
2+
3+
public class YamlDemoApplication {
4+
public static void main(String[] args) {
5+
System.out.println("Hello Maven Polyglot YAML !");
6+
}
7+
}

0 commit comments

Comments
 (0)