|
| 1 | +/* |
| 2 | + * Copyright 2026 astonbitecode Licensed under the Apache License, Version 2.0 (the "License"); you |
| 3 | + * may not use this file except in compliance with the License. You may obtain a copy of the License |
| 4 | + * at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 9 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 10 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 11 | + * the License. |
| 12 | + */ |
| 13 | +package org.astonbitecode.j4rs.api.deploy; |
| 14 | + |
| 15 | +import java.io.File; |
| 16 | +import java.io.IOException; |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.HashMap; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Map; |
| 21 | +import org.apache.maven.model.Dependency; |
| 22 | +import org.apache.maven.model.Model; |
| 23 | +import org.apache.maven.model.Repository; |
| 24 | +import org.apache.maven.model.building.DefaultModelBuilderFactory; |
| 25 | +import org.apache.maven.model.building.DefaultModelBuildingRequest; |
| 26 | +import org.apache.maven.model.building.FileModelSource; |
| 27 | +import org.apache.maven.model.building.ModelBuilder; |
| 28 | +import org.apache.maven.model.building.ModelBuildingResult; |
| 29 | +import org.apache.maven.model.building.ModelSource; |
| 30 | +import org.apache.maven.model.resolution.InvalidRepositoryException; |
| 31 | +import org.apache.maven.model.resolution.ModelResolver; |
| 32 | +import org.apache.maven.model.resolution.UnresolvableModelException; |
| 33 | + |
| 34 | +public class MavenDeployer implements MavenDeployerApi { |
| 35 | + private static final String POM_TYPE = "pom"; |
| 36 | + private final SimpleMavenDeployer simpleMavenDeployer; |
| 37 | + private final Map<String, SimpleMavenDeployer> additionalDeployers = new HashMap<>(); |
| 38 | + |
| 39 | + public MavenDeployer() { |
| 40 | + this.simpleMavenDeployer = new SimpleMavenDeployer(); |
| 41 | + } |
| 42 | + |
| 43 | + public MavenDeployer(String deployTarget) { |
| 44 | + this.simpleMavenDeployer = new SimpleMavenDeployer(deployTarget); |
| 45 | + } |
| 46 | + |
| 47 | + public MavenDeployer(String repoBase, String deployTarget) { |
| 48 | + this.simpleMavenDeployer = new SimpleMavenDeployer(repoBase, deployTarget); |
| 49 | + } |
| 50 | + |
| 51 | + public MavenDeployer(String repoBase, boolean checkLocalCache, String deployTarget) { |
| 52 | + this.simpleMavenDeployer = new SimpleMavenDeployer(repoBase, checkLocalCache, deployTarget); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void deploy(String groupId, String artifactId, String version, String qualifier) throws IOException { |
| 57 | + this.deploy(groupId, artifactId, version, qualifier, "jar"); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void deploy(String groupId, String artifactId, String version, String qualifier, String artifactType) throws IOException { |
| 62 | + if (!DeployUtils.artifactExists(groupId, artifactId, version, qualifier, artifactType, artifactType)) { |
| 63 | + doDeploy(groupId, artifactId, version, qualifier, artifactType, gatherAllDeployers()); |
| 64 | + if (!artifactType.equals(POM_TYPE)) { |
| 65 | + // For pom types the qualifiers should not be defined |
| 66 | + doDeploy(groupId, artifactId, version, "", POM_TYPE, gatherAllDeployers()); |
| 67 | + } |
| 68 | + // For pom types the qualifiers should not be defined |
| 69 | + String pomArtifactName = DeployUtils.generateArtifactName(artifactId, version, "", POM_TYPE); |
| 70 | + String pathString = getDeployTarget() + File.separator + pomArtifactName; |
| 71 | + try { |
| 72 | + File pomFile = new File(pathString); |
| 73 | + List<Dependency> dependencies = parsePom(pomFile); |
| 74 | + if (dependencies != null) { |
| 75 | + for (Dependency dep : dependencies) { |
| 76 | + if (!dep.getArtifactId().contains("j4rs") && dep.getType().equals(artifactType) && !dep.getScope().equals("test") && !dep.getScope().equals("provided")) { |
| 77 | + // Deploy only for the needed os |
| 78 | + if (dep.getClassifier() == null || dep.getClassifier().length() == 0 || dep.getClassifier().contains(System.getProperty("os.name").toLowerCase())) { |
| 79 | + deploy(dep.getGroupId(), dep.getArtifactId(), dep.getVersion(), dep.getClassifier(), artifactType); |
| 80 | + } |
| 81 | + } |
| 82 | + if (!POM_TYPE.equals(artifactType)) { |
| 83 | + pomFile.delete(); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + } catch (Exception error) { |
| 88 | + error.printStackTrace(); |
| 89 | + throw new IOException(error); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + List<Dependency> parsePom(File pomFile) throws Exception { |
| 95 | + final DefaultModelBuildingRequest modelBuildingRequest = new DefaultModelBuildingRequest().setPomFile(pomFile); |
| 96 | + modelBuildingRequest.setModelResolver(new J4rsMavenModelResolver()); |
| 97 | + modelBuildingRequest.setSystemProperties(System.getProperties()); |
| 98 | + ModelBuilder modelBuilder = new DefaultModelBuilderFactory().newInstance(); |
| 99 | + ModelBuildingResult modelBuildingResult = modelBuilder.build(modelBuildingRequest); |
| 100 | + |
| 101 | + Model model = modelBuildingResult.getEffectiveModel(); |
| 102 | + List<Dependency> deps = model.getDependencies(); |
| 103 | + return deps; |
| 104 | + } |
| 105 | + |
| 106 | + public String getRepoBase() { |
| 107 | + return simpleMavenDeployer.getRepoBase(); |
| 108 | + } |
| 109 | + |
| 110 | + public String getDeployTarget() { |
| 111 | + return simpleMavenDeployer.getDeployTarget(); |
| 112 | + } |
| 113 | + |
| 114 | + private List<SimpleMavenDeployer> gatherAllDeployers() { |
| 115 | + List<SimpleMavenDeployer> deployers = new ArrayList<>(); |
| 116 | + deployers.add(simpleMavenDeployer); |
| 117 | + deployers.addAll(additionalDeployers.values()); |
| 118 | + return deployers; |
| 119 | + } |
| 120 | + |
| 121 | + void doDeploy(String groupId, String artifactId, String version, String qualifier, String artifactType, List<SimpleMavenDeployer> deployers) throws IOException { |
| 122 | + if (deployers != null && deployers.size() > 0) { |
| 123 | + try { |
| 124 | + deployers.get(0).deploy(groupId, artifactId, version, qualifier, artifactType); |
| 125 | + } catch (IOException error) { |
| 126 | + if (deployers.size() == 1) { |
| 127 | + throw error; |
| 128 | + } else { |
| 129 | + List<SimpleMavenDeployer> reducedDeployers = deployers.subList(1, deployers.size() - 1); |
| 130 | + doDeploy(groupId, artifactId, version, qualifier, artifactType, reducedDeployers); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + private class J4rsMavenModelResolver implements ModelResolver { |
| 137 | + |
| 138 | + @Override |
| 139 | + public ModelSource resolveModel(String groupId, String artifactId, String version) throws UnresolvableModelException { |
| 140 | + |
| 141 | + try { |
| 142 | + doDeploy(groupId, artifactId, version, "", POM_TYPE, gatherAllDeployers()); |
| 143 | + String pomArtifactName = DeployUtils.generateArtifactName(artifactId, version, "", POM_TYPE); |
| 144 | + String pathString = getDeployTarget() + File.separator + pomArtifactName; |
| 145 | + return new FileModelSource(new File(pathString)); |
| 146 | + } catch (IOException error) { |
| 147 | + throw new UnresolvableModelException("Could not resolve model", groupId, artifactId, version, error); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public void addRepository(Repository repository) throws InvalidRepositoryException { |
| 153 | + additionalDeployers.put(repository.getUrl(), new SimpleMavenDeployer(getRepoBase(), repository.getUrl())); |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public ModelResolver newCopy() { |
| 158 | + return new J4rsMavenModelResolver(); |
| 159 | + } |
| 160 | + |
| 161 | + } |
| 162 | +} |
0 commit comments