@Option(name = "finalName",
description = "The final artifact name of the new project") final String finalName,
final PipeOut out
) throws IOException
{
DirectoryResource dir = null;
String javaPackage = suggestedJavaPackage;
if (!getValidPackagingTypes().contains(type))
{
throw new RuntimeException("Unsupported packaging type: " + type);
}
// FORGE-571
if (javaPackage == null)
{
javaPackage = "com.example." + name;
}
boolean skipFolderPrompt = false;
try
{
if (projectFolder instanceof FileResource<?>)
{
if (!projectFolder.exists())
{
((FileResource<?>) projectFolder).mkdirs();
dir = projectFolder.reify(DirectoryResource.class);
skipFolderPrompt = true;
}
else if (projectFolder instanceof DirectoryResource)
{
dir = (DirectoryResource) projectFolder;
skipFolderPrompt = true;
}
else
{
ShellMessages.error(out, "File exists but is not a directory [" + projectFolder.getFullyQualifiedName()
+ "]");
}
}
if (dir == null)
{
dir = shell.getCurrentDirectory().getChildDirectory(name);
}
}
catch (ResourceException e)
{
}
if (!skipFolderPrompt && (projectFactory.containsProject(dir)
|| !shell.promptBoolean("Use [" + dir.getFullyQualifiedName() + "] as project directory?")))
{
if (projectFactory.containsProject(dir))
{
ShellMessages.error(out, "[" + dir.getFullyQualifiedName()
+ "] already contains a project; please use a different folder.");
}
if (shell.getCurrentResource() == null)
{
dir = ResourceUtil.getContextDirectory(factory.getResourceFrom(Files.getWorkingDirectory()));
}
else
{
dir = shell.getCurrentDirectory();
}
FileResource<?> newDir;
do
{
newDir = shell.getCurrentDirectory();
shell.println();
if (!projectFactory.containsProject(newDir.reify(DirectoryResource.class)))
{
newDir = shell.promptFile(
"Where would you like to create the project? [Press ENTER to use the current directory: "
+ newDir + "]", dir);
}
else
{
newDir = shell.promptFile("Where would you like to create the project?");
}
if (!newDir.exists())
{
newDir.mkdirs();
newDir = newDir.reify(DirectoryResource.class);
}
else if (newDir.isDirectory() && !projectFactory.containsProject(newDir.reify(DirectoryResource.class)))
{
newDir = newDir.reify(DirectoryResource.class);
}
else
{
ShellMessages.error(out, "That folder already contains a project [" + newDir.getFullyQualifiedName()
+ "], please select a different location.");
newDir = null;
}
}
while ((newDir == null) || !(newDir instanceof DirectoryResource));
dir = (DirectoryResource) newDir;
}
if (!dir.exists())
{
dir.mkdirs();
}
Project project = null;
if (type.equals(PackagingType.JAR) || type.equals(PackagingType.BUNDLE))
{
project = projectFactory.createProject(dir,
DependencyFacet.class,
MetadataFacet.class,
JavaSourceFacet.class,
ResourceFacet.class);
}
else if (type.equals(PackagingType.WAR))
{
project = projectFactory.createProject(dir,
DependencyFacet.class,
MetadataFacet.class,
WebResourceFacet.class,
JavaSourceFacet.class,
ResourceFacet.class);
}
else
{
project = projectFactory.createProject(dir,
DependencyFacet.class,
MetadataFacet.class);
}
DirectoryResource parentDir = project.getProjectRoot().getParent().reify(DirectoryResource.class);
if (parentDir != null)
{
for (ProjectAssociationProvider provider : providers)
{
if (provider.canAssociate(project, parentDir)
&& shell.promptBoolean("Add new project as a sub-project of [" + parentDir.getFullyQualifiedName()
+ "]?"))
{
provider.associate(project, parentDir);
}
}
}
MetadataFacet meta = project.getFacet(MetadataFacet.class);
meta.setProjectName(name);
meta.setTopLevelPackage(javaPackage);
PackagingFacet packaging = project.getFacet(PackagingFacet.class);
packaging.setPackagingType(type);
DependencyFacet deps = project.getFacet(DependencyFacet.class);
deps.addRepository(KnownRepository.JBOSS_NEXUS);
if (packaging.getPackagingType().equals(PackagingType.JAR) && createMain)
{
project.getFacet(JavaSourceFacet.class).saveJavaSource(JavaParser
.create(JavaClass.class)
.setPackage(javaPackage)
.setName("Main")
.addMethod("public static void main(String[] args) {}")
.setBody("System.out.println(\"Hi there! I was forged as part of the project you call " + name
+ ".\");")
.getOrigin());
}
if (project.hasFacet(JavaSourceFacet.class))
{
DirectoryResource sourceFolder = project.getFacet(JavaSourceFacet.class).getSourceFolder();
createTopLevelPackage(sourceFolder, javaPackage);
}
if (finalName != null)
{