You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@maven.apache.org by "Ilker M. Karakas" <il...@gmail.com> on 2008/05/25 00:13:10 UTC

Problems trying to 'bootstrap' Spring in a Maven Plug-In

Hi -

I am new to writing Maven plug-ins.

I am fiddling with a plug-in, where I do have to make use of Spring  
Beans (therefore I have to initialize Spring in a programmatic  
fashion). I am invoking this plug-in from another project's pom. My  
problem is, in my plug-in, I could not so far manage to have Spring  
find its 'applicationcontext.xml' file ( I am using the  
ClassPathXmlApplicationContext constructor,and the plug-is is  
complaining that it is not able to find the applicationcontext file on  
the classpath.

So, is there a way I can override the the classpath of the thread that  
starts my plugin??

To make things more understandable, here is a chunk from my project  
pom (where I call my plug-in)

...........
<executions>
	<execution>
		<phase>generate-sources</phase>
			<goals>
				<goal>
					CreateTemplateWebProject
				</goal>
			</goals>
	</execution>
</executions>
<configuration>
	<deploymentDirectory>
		/Users/ilkerkarakas/temp
	</deploymentDirectory>
</configuration>
.............

And here is an excerpt from my plug-in:

............
	public void execute() throws MojoExecutionException {
		File f = deploymentDirectory;
		FileWriter w = null;

		if (getLog().isInfoEnabled()) {
			getLog().info(
					"Starting the build, deploymentDirectory is: ["
							+ deploymentDirectory.getAbsolutePath() + "]");
		}

		try {
			if (f.exists() && !PluginUtils.deleteDirectory(f)) {
				throw new MojoExecutionException(
						"Could not empty target directory");
			} else {
				f.mkdirs();
			}
			String[] directoryTree = new String[] { "src/main/java",
					"src/main/resources", "src/test/java",
					"src/test/resources", "WebContent" };
			for (String dir : directoryTree) {
				new File(f, dir).mkdirs();
			}
			if (getLog().isInfoEnabled()) {
				getLog().info("Initializing Spring...");
			}

			// Start-up the Spring App. Context  (It blows here...)
			appContext = new ClassPathXmlApplicationContext(
					new String[] { "ApplicationContext.xml" });

...............

And here is the error:

INFO] Scanning for projects...
[INFO]  
----------------------------------------------------------------------------
[INFO] Building Web (Maven) Testing
[INFO]    task-segment: [generate-sources]
[INFO]  
----------------------------------------------------------------------------
[INFO] ictweb: CreateTemplateWebProject
[INFO] Starting the build, deploymentDirectory is: [/Users/ 
ilkerkarakas/temp/]
[INFO] Initializing Sring...
log4j:WARN No appenders could be found for logger  
(org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
[ERROR]  IOException parsing XML document from class path resource  
[ApplicationContext.xml]; nested exception is  
java.io.FileNotFoundException: class path resource  
[ApplicationContext.xml] cannot be opened because it does not exist
[INFO] Finished the build...
org.apache.maven.plugin.MojoExecutionException: Error
	at web.JSFProjectCreationMojo.execute(JSFProjectCreationMojo.java:82)
	at  
org 
.apache 
.maven 
.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:578)
	at  
org 
.apache 
.maven 
.lifecycle 
.DefaultLifecycleExecutor 
.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:508)
	at  
org 
.apache 
.maven 
.lifecycle 
.DefaultLifecycleExecutor 
.executeTaskSegments(DefaultLifecycleExecutor.java:328)
	at  
org 
.apache 
.maven 
.lifecycle 
.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:151)
	at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:220)
	at org.apache.maven.embedder.MavenEmbedder.execute(MavenEmbedder.java: 
813)
	at  
org.maven.ide.eclipse.embedder.Maven2Executor.main(Maven2Executor.java: 
85)

-----------

Thanks
-ilker