Skip to content

Commit c4a0633

Browse files
aserkesromain-grecourt
authored andcommitted
check of compatibility archetype version and cli (draft)
helidon init compatibility support fix check compatibility in ArchetypeEngineV2.java add logs to test add logs to test, fix copyright fix check compatibility in ArchetypeEngineV2 use maven-remote-resources-plugin to share resources between modules refactoring refactoring code change after review fix IT test Signed-off-by: aserkes <[email protected]>
1 parent 582f2f2 commit c4a0633

File tree

19 files changed

+1898
-58
lines changed

19 files changed

+1898
-58
lines changed

archetype/engine-v2/pom.xml

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@
4949
<artifactId>helidon-build-common-xml</artifactId>
5050
<version>${project.version}</version>
5151
</dependency>
52+
<dependency>
53+
<groupId>io.helidon.build-tools.common</groupId>
54+
<artifactId>helidon-build-common-maven</artifactId>
55+
<version>${project.version}</version>
56+
</dependency>
5257
<dependency>
5358
<groupId>com.github.spullara.mustache.java</groupId>
5459
<artifactId>compiler</artifactId>
@@ -77,25 +82,34 @@
7782
</dependencies>
7883

7984
<build>
85+
<resources>
86+
<resource>
87+
<directory>${project.basedir}/src/main/resources</directory>
88+
</resource>
89+
<resource>
90+
<directory>${project.basedir}/src/main/schema</directory>
91+
<targetPath>${project.build.outputDirectory}/schemas</targetPath>
92+
<includes>
93+
<include>*.xsd</include>
94+
</includes>
95+
</resource>
96+
</resources>
8097
<plugins>
8198
<plugin>
82-
<groupId>org.codehaus.mojo</groupId>
83-
<artifactId>build-helper-maven-plugin</artifactId>
99+
<groupId>org.apache.maven.plugins</groupId>
100+
<artifactId>maven-remote-resources-plugin</artifactId>
84101
<executions>
85102
<execution>
86-
<id>attach-artifacts</id>
87-
<phase>package</phase>
103+
<id>bundle-resources</id>
104+
<phase>process-resources</phase>
88105
<goals>
89-
<goal>attach-artifact</goal>
106+
<goal>bundle</goal>
90107
</goals>
91108
<configuration>
92-
<artifacts>
93-
<artifact>
94-
<file>src/main/schema/archetype.xsd</file>
95-
<type>xsd</type>
96-
<classifier>schema-2.0</classifier>
97-
</artifact>
98-
</artifacts>
109+
<resourcesDirectory>${project.build.outputDirectory}</resourcesDirectory>
110+
<includes>
111+
<include>**/schemas/**/*.xsd</include>
112+
</includes>
99113
</configuration>
100114
</execution>
101115
</executions>

archetype/engine-v2/src/main/java/io/helidon/build/archetype/engine/v2/ArchetypeEngineV2.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,21 @@
1717
package io.helidon.build.archetype.engine.v2;
1818

1919
import java.io.File;
20+
import java.io.IOException;
2021
import java.nio.file.FileSystem;
22+
import java.nio.file.Files;
2123
import java.nio.file.Path;
2224
import java.util.Map;
2325
import java.util.function.Function;
26+
import java.util.jar.Manifest;
2427

2528
import io.helidon.build.archetype.engine.v2.ast.Script;
2629
import io.helidon.build.archetype.engine.v2.context.Context;
2730
import io.helidon.build.archetype.engine.v2.context.ContextSerializer;
2831
import io.helidon.build.common.FileUtils;
32+
import io.helidon.build.common.RequirementFailure;
33+
import io.helidon.build.common.maven.MavenVersion;
34+
import io.helidon.build.common.maven.VersionRange;
2935

3036
import static java.util.Objects.requireNonNull;
3137

@@ -37,6 +43,8 @@ public class ArchetypeEngineV2 {
3743
private static final String ENTRYPOINT = "main.xml";
3844
private static final String ARTIFACT_ID = "artifactId";
3945

46+
private static final VersionRange COMPATIBLE_SCHEMA_RANGE = VersionRange.createFromVersionSpec("[2.0.0,3.0.0)");
47+
4048
private final Path cwd;
4149
private final InputResolver inputResolver;
4250
private final Map<String, String> externalValues;
@@ -46,6 +54,7 @@ public class ArchetypeEngineV2 {
4654
private final File outputPropsFile;
4755

4856
private ArchetypeEngineV2(Builder builder) {
57+
checkCompatability(builder.cwd);
4958
this.cwd = builder.cwd;
5059
this.inputResolver = builder.inputResolver;
5160
this.externalValues = builder.externalValues;
@@ -55,6 +64,32 @@ private ArchetypeEngineV2(Builder builder) {
5564
this.outputPropsFile = builder.outputPropsFile;
5665
}
5766

67+
private void checkCompatability(Path cwd) {
68+
Path manifestPath = cwd.resolve("META-INF/MANIFEST.MF");
69+
if (!Files.exists(manifestPath)) {
70+
//it is not an archetype archive
71+
return;
72+
}
73+
try {
74+
Manifest manifest = new Manifest(Files.newInputStream(manifestPath));
75+
String maxVersion = manifest.getMainAttributes().getValue("archetype-schema-version");
76+
if (maxVersion == null) {
77+
//it is a some previous version of archetype archive
78+
return;
79+
}
80+
MavenVersion mavenVersion = MavenVersion.toMavenVersion(maxVersion);
81+
if (!COMPATIBLE_SCHEMA_RANGE.containsVersion(mavenVersion)) {
82+
throw new RequirementFailure(
83+
String.format("Version of schema %s is not compatible with the current version of "
84+
+ "the Archetype engine (v2). The version of schema must be in range %s",
85+
maxVersion,
86+
COMPATIBLE_SCHEMA_RANGE));
87+
}
88+
} catch (IOException e) {
89+
throw new RequirementFailure(e.getMessage());
90+
}
91+
}
92+
5893
/**
5994
* Generate a project.
6095
*

archetype/engine-v2/src/main/java/module-info.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, 2022 Oracle and/or its affiliates.
2+
* Copyright (c) 2021, 2023 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,6 +26,7 @@
2626
requires io.helidon.build.common.ansi;
2727
requires io.helidon.build.common.xml;
2828
requires com.github.mustachejava;
29+
requires io.helidon.build.common.maven;
2930

3031
exports io.helidon.build.archetype.engine.v2;
3132
exports io.helidon.build.archetype.engine.v2.ast;

0 commit comments

Comments
 (0)