use of org.apache.maven.archetype.metadata.ArchetypeDescriptor in project maven-archetype by apache.
the class DefaultArchetypeGenerationConfigurator2Test method setUp.
public void setUp() throws Exception {
super.setUp();
configurator = (DefaultArchetypeGenerationConfigurator) lookup(ArchetypeGenerationConfigurator.ROLE);
ProjectBuildingRequest buildingRequest = null;
// MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
// repositorySession.setLocalRepositoryManager( new SimpleLocalRepositoryManager( localRepository.getBasedir() ) );
// buildingRequest.setRepositorySession( repositorySession );
// request.setProjectBuildingRequest( buildingRequest );
MockControl control = MockControl.createControl(ArchetypeArtifactManager.class);
control.setDefaultMatcher(MockControl.ALWAYS_MATCHER);
ArchetypeArtifactManager manager = (ArchetypeArtifactManager) control.getMock();
manager.exists("archetypeGroupId", "archetypeArtifactId", "archetypeVersion", null, null, null, buildingRequest);
control.setReturnValue(true);
manager.isFileSetArchetype("archetypeGroupId", "archetypeArtifactId", "archetypeVersion", null, null, null, buildingRequest);
control.setReturnValue(true);
manager.isOldArchetype("archetypeGroupId", "archetypeArtifactId", "archetypeVersion", null, null, null, buildingRequest);
control.setReturnValue(false);
manager.getFileSetArchetypeDescriptor("archetypeGroupId", "archetypeArtifactId", "archetypeVersion", null, null, null, buildingRequest);
ArchetypeDescriptor descriptor = new ArchetypeDescriptor();
RequiredProperty groupId = new RequiredProperty();
groupId.setKey("groupId");
groupId.setDefaultValue("com.example.${groupName}");
RequiredProperty artifactId = new RequiredProperty();
artifactId.setKey("artifactId");
artifactId.setDefaultValue("${serviceName}");
RequiredProperty thePackage = new RequiredProperty();
thePackage.setKey("package");
thePackage.setDefaultValue("com.example.${groupName}");
RequiredProperty groupName = new RequiredProperty();
groupName.setKey("groupName");
groupName.setDefaultValue(null);
RequiredProperty serviceName = new RequiredProperty();
serviceName.setKey("serviceName");
serviceName.setDefaultValue(null);
descriptor.addRequiredProperty(groupId);
descriptor.addRequiredProperty(artifactId);
descriptor.addRequiredProperty(thePackage);
descriptor.addRequiredProperty(groupName);
descriptor.addRequiredProperty(serviceName);
control.setReturnValue(descriptor);
control.replay();
configurator.setArchetypeArtifactManager(manager);
}
use of org.apache.maven.archetype.metadata.ArchetypeDescriptor in project maven-archetype by apache.
the class DefaultFilesetArchetypeGenerator method generateArchetype.
public void generateArchetype(ArchetypeGenerationRequest request, File archetypeFile) throws UnknownArchetype, ArchetypeNotConfigured, ProjectDirectoryExists, PomFileExists, OutputFileExists, ArchetypeGenerationFailure {
ClassLoader old = Thread.currentThread().getContextClassLoader();
try {
ArchetypeDescriptor archetypeDescriptor = archetypeArtifactManager.getFileSetArchetypeDescriptor(archetypeFile);
if (!isArchetypeConfigured(archetypeDescriptor, request)) {
if (request.isInteractiveMode()) {
throw new ArchetypeNotConfigured("No archetype was chosen.", null);
}
StringBuffer exceptionMessage = new StringBuffer("Archetype " + request.getArchetypeGroupId() + ":" + request.getArchetypeArtifactId() + ":" + request.getArchetypeVersion() + " is not configured");
List<String> missingProperties = new ArrayList<String>(0);
for (RequiredProperty requiredProperty : archetypeDescriptor.getRequiredProperties()) {
if (StringUtils.isEmpty(request.getProperties().getProperty(requiredProperty.getKey()))) {
exceptionMessage.append("\n\tProperty " + requiredProperty.getKey() + " is missing.");
missingProperties.add(requiredProperty.getKey());
}
}
throw new ArchetypeNotConfigured(exceptionMessage.toString(), missingProperties);
}
Context context = prepareVelocityContext(request);
String packageName = request.getPackage();
String artifactId = request.getArtifactId();
File outputDirectoryFile = new File(request.getOutputDirectory(), artifactId);
File basedirPom = new File(request.getOutputDirectory(), Constants.ARCHETYPE_POM);
File pom = new File(outputDirectoryFile, Constants.ARCHETYPE_POM);
List<String> archetypeResources = archetypeArtifactManager.getFilesetArchetypeResources(archetypeFile);
ZipFile archetypeZipFile = archetypeArtifactManager.getArchetypeZipFile(archetypeFile);
ClassLoader archetypeJarLoader = archetypeArtifactManager.getArchetypeJarLoader(archetypeFile);
Thread.currentThread().setContextClassLoader(archetypeJarLoader);
if (archetypeDescriptor.isPartial()) {
getLogger().debug("Processing partial archetype " + archetypeDescriptor.getName());
if (outputDirectoryFile.exists()) {
if (!pom.exists()) {
throw new PomFileExists("This is a partial archetype and the pom.xml file doesn't exist.");
}
processPomWithMerge(context, pom, "");
processArchetypeTemplatesWithWarning(archetypeDescriptor, archetypeResources, archetypeZipFile, "", context, packageName, outputDirectoryFile);
} else {
if (basedirPom.exists()) {
processPomWithMerge(context, basedirPom, "");
processArchetypeTemplatesWithWarning(archetypeDescriptor, archetypeResources, archetypeZipFile, "", context, packageName, new File(request.getOutputDirectory()));
} else {
processPom(context, pom, "");
processArchetypeTemplates(archetypeDescriptor, archetypeResources, archetypeZipFile, "", context, packageName, outputDirectoryFile);
}
}
if (archetypeDescriptor.getModules().size() > 0) {
getLogger().info("Modules ignored in partial mode");
}
} else {
getLogger().debug("Processing complete archetype " + archetypeDescriptor.getName());
if (outputDirectoryFile.exists() && pom.exists()) {
throw new ProjectDirectoryExists("A Maven 2 project already exists in the directory " + outputDirectoryFile.getPath());
}
if (outputDirectoryFile.exists()) {
getLogger().warn("The directory " + outputDirectoryFile.getPath() + " already exists.");
}
context.put("rootArtifactId", artifactId);
processFilesetModule(artifactId, artifactId, archetypeResources, pom, archetypeZipFile, "", basedirPom, outputDirectoryFile, packageName, archetypeDescriptor, context);
}
String postGenerationScript = archetypeArtifactManager.getPostGenerationScript(archetypeFile);
if (postGenerationScript != null) {
getLogger().info("Executing " + Constants.ARCHETYPE_POST_GENERATION_SCRIPT + " post-generation script");
Binding binding = new Binding();
final Properties archetypeGeneratorProperties = new Properties();
archetypeGeneratorProperties.putAll(System.getProperties());
if (request.getProperties() != null) {
archetypeGeneratorProperties.putAll(request.getProperties());
}
for (Map.Entry<Object, Object> entry : archetypeGeneratorProperties.entrySet()) {
binding.setVariable(entry.getKey().toString(), entry.getValue());
}
binding.setVariable("request", request);
GroovyShell shell = new GroovyShell(binding);
shell.evaluate(postGenerationScript);
}
// ----------------------------------------------------------------------
if (getLogger().isInfoEnabled()) {
getLogger().info("Project created from Archetype in dir: " + outputDirectoryFile.getAbsolutePath());
}
} catch (FileNotFoundException ex) {
throw new ArchetypeGenerationFailure(ex);
} catch (IOException ex) {
throw new ArchetypeGenerationFailure(ex);
} catch (XmlPullParserException ex) {
throw new ArchetypeGenerationFailure(ex);
} catch (DocumentException ex) {
throw new ArchetypeGenerationFailure(ex);
} catch (ArchetypeGenerationFailure ex) {
throw new ArchetypeGenerationFailure(ex);
} catch (InvalidPackaging ex) {
throw new ArchetypeGenerationFailure(ex);
} finally {
Thread.currentThread().setContextClassLoader(old);
}
}
use of org.apache.maven.archetype.metadata.ArchetypeDescriptor in project maven-archetype by apache.
the class FilesetArchetypeCreator method createArchetype.
public void createArchetype(ArchetypeCreationRequest request, ArchetypeCreationResult result) {
MavenProject project = request.getProject();
List<String> languages = request.getLanguages();
List<String> filtereds = request.getFiltereds();
String defaultEncoding = request.getDefaultEncoding();
boolean preserveCData = request.isPreserveCData();
boolean keepParent = request.isKeepParent();
boolean partialArchetype = request.isPartialArchetype();
File outputDirectory = request.getOutputDirectory();
File basedir = project.getBasedir();
Properties properties = new Properties();
Properties configurationProperties = new Properties();
if (request.getProperties() != null) {
properties.putAll(request.getProperties());
configurationProperties.putAll(request.getProperties());
}
extractPropertiesFromProject(project, properties, configurationProperties, request.getPackageName());
if (outputDirectory == null) {
getLogger().debug("No output directory defined, using default: " + DEFAULT_OUTPUT_DIRECTORY);
outputDirectory = FileUtils.resolveFile(basedir, DEFAULT_OUTPUT_DIRECTORY);
}
outputDirectory.mkdirs();
getLogger().debug("Creating archetype in " + outputDirectory);
try {
File archetypePomFile = createArchetypeProjectPom(project, request.getProjectBuildingRequest(), configurationProperties, outputDirectory);
File archetypeResourcesDirectory = new File(outputDirectory, getTemplateOutputDirectory());
File archetypeFilesDirectory = new File(archetypeResourcesDirectory, Constants.ARCHETYPE_RESOURCES);
getLogger().debug("Archetype's files output directory " + archetypeFilesDirectory);
File archetypeDescriptorFile = new File(archetypeResourcesDirectory, Constants.ARCHETYPE_DESCRIPTOR);
archetypeDescriptorFile.getParentFile().mkdirs();
File archetypePostGenerationScript = new File(archetypeResourcesDirectory, Constants.ARCHETYPE_POST_GENERATION_SCRIPT);
archetypePostGenerationScript.getParentFile().mkdirs();
if (request.getProject().getBuild() != null && CollectionUtils.isNotEmpty(request.getProject().getBuild().getResources())) {
for (Resource resource : request.getProject().getBuild().getResources()) {
File inputFile = new File(resource.getDirectory() + File.separator + Constants.ARCHETYPE_POST_GENERATION_SCRIPT);
if (inputFile.exists()) {
FileUtils.copyFile(inputFile, archetypePostGenerationScript);
}
}
}
getLogger().debug("Starting archetype's descriptor " + project.getArtifactId());
ArchetypeDescriptor archetypeDescriptor = new ArchetypeDescriptor();
archetypeDescriptor.setName(project.getArtifactId());
archetypeDescriptor.setPartial(partialArchetype);
addRequiredProperties(archetypeDescriptor, properties);
// TODO ensure reverseProperties contains NO dotted properties
Properties reverseProperties = getReversedProperties(archetypeDescriptor, properties);
// reverseProperties.remove( Constants.GROUP_ID );
// TODO ensure pomReversedProperties contains NO dotted properties
Properties pomReversedProperties = getReversedProperties(archetypeDescriptor, properties);
// pomReversedProperties.remove( Constants.PACKAGE );
String packageName = configurationProperties.getProperty(Constants.PACKAGE);
Model pom = pomManager.readPom(FileUtils.resolveFile(basedir, Constants.ARCHETYPE_POM));
List<String> excludePatterns = configurationProperties.getProperty(Constants.EXCLUDE_PATTERNS) != null ? Arrays.asList(StringUtils.split(configurationProperties.getProperty(Constants.EXCLUDE_PATTERNS), ",")) : Collections.<String>emptyList();
List<String> fileNames = resolveFileNames(pom, basedir, excludePatterns);
if (getLogger().isDebugEnabled()) {
getLogger().debug("Scanned for files " + fileNames.size());
for (String name : fileNames) {
getLogger().debug("- " + name);
}
}
List<FileSet> filesets = resolveFileSets(packageName, fileNames, languages, filtereds, defaultEncoding);
getLogger().debug("Resolved filesets for " + archetypeDescriptor.getName());
archetypeDescriptor.setFileSets(filesets);
createArchetypeFiles(reverseProperties, filesets, packageName, basedir, archetypeFilesDirectory, defaultEncoding, excludePatterns);
getLogger().debug("Created files for " + archetypeDescriptor.getName());
setParentArtifactId(reverseProperties, configurationProperties.getProperty(Constants.ARTIFACT_ID));
for (String moduleId : pom.getModules()) {
String rootArtifactId = configurationProperties.getProperty(Constants.ARTIFACT_ID);
String moduleIdDirectory = moduleId;
if (moduleId.indexOf(rootArtifactId) >= 0) {
moduleIdDirectory = StringUtils.replace(moduleId, rootArtifactId, "__rootArtifactId__");
}
getLogger().debug("Creating module " + moduleId);
ModuleDescriptor moduleDescriptor = createModule(reverseProperties, rootArtifactId, moduleId, packageName, FileUtils.resolveFile(basedir, moduleId), new File(archetypeFilesDirectory, moduleIdDirectory), languages, filtereds, defaultEncoding, preserveCData, keepParent);
archetypeDescriptor.addModule(moduleDescriptor);
getLogger().debug("Added module " + moduleDescriptor.getName() + " in " + archetypeDescriptor.getName());
}
restoreParentArtifactId(reverseProperties, null);
restoreArtifactId(reverseProperties, configurationProperties.getProperty(Constants.ARTIFACT_ID));
createPoms(pom, configurationProperties.getProperty(Constants.ARTIFACT_ID), configurationProperties.getProperty(Constants.ARTIFACT_ID), archetypeFilesDirectory, basedir, pomReversedProperties, preserveCData, keepParent);
getLogger().debug("Created Archetype " + archetypeDescriptor.getName() + " template pom(s)");
Writer out = null;
try {
out = WriterFactory.newXmlWriter(archetypeDescriptorFile);
ArchetypeDescriptorXpp3Writer writer = new ArchetypeDescriptorXpp3Writer();
writer.write(out, archetypeDescriptor);
getLogger().debug("Archetype " + archetypeDescriptor.getName() + " descriptor written");
} finally {
IOUtil.close(out);
}
createArchetypeBasicIt(archetypeDescriptor, outputDirectory);
// Copy archetype integration tests.
File archetypeIntegrationTestInputFolder = new File(basedir, Constants.SRC + File.separator + "it" + File.separator + "projects");
File archetypeIntegrationTestOutputFolder = new File(outputDirectory, Constants.SRC + File.separator + Constants.TEST + File.separator + Constants.RESOURCES + File.separator + "projects");
if (archetypeIntegrationTestInputFolder.exists()) {
getLogger().info("Copying: " + archetypeIntegrationTestInputFolder.getAbsolutePath() + " into " + archetypeIntegrationTestOutputFolder.getAbsolutePath());
FileUtils.copyDirectoryStructure(archetypeIntegrationTestInputFolder, archetypeIntegrationTestOutputFolder);
}
InvocationRequest internalRequest = new DefaultInvocationRequest();
internalRequest.setPomFile(archetypePomFile);
internalRequest.setGoals(Collections.singletonList(request.getPostPhase()));
if (request.getLocalRepository() != null) {
internalRequest.setLocalRepositoryDirectory(new File(request.getLocalRepository().getBasedir()));
}
InvocationResult invokerResult = invoker.execute(internalRequest);
if (invokerResult.getExitCode() != 0) {
if (invokerResult.getExecutionException() != null) {
throw invokerResult.getExecutionException();
} else {
throw new Exception("Invoker process ended with result different than 0!");
}
}
} catch (Exception e) {
result.setCause(e);
}
}
Aggregations