------ Introspecting a class ------ Mark Hobson ------ 19 May 2008 ------ ~~ Licensed to the Apache Software Foundation (ASF) under one ~~ or more contributor license agreements. See the NOTICE file ~~ distributed with this work for additional information ~~ regarding copyright ownership. The ASF licenses this file ~~ to you under the Apache License, Version 2.0 (the ~~ "License"); you may not use this file except in compliance ~~ with the License. You may obtain a copy of the License at ~~ ~~ http://www.apache.org/licenses/LICENSE-2.0 ~~ ~~ Unless required by applicable law or agreed to in writing, ~~ software distributed under the License is distributed on an ~~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY ~~ KIND, either express or implied. See the License for the ~~ specific language governing permissions and limitations ~~ under the License. ~~ NOTE: For help with the syntax of this file, see: ~~ http://maven.apache.org/doxia/references/apt-format.html Introspecting a class Maven Runtime can introspect a class to obtain its related Maven project's metadata. Note that this relies on the Maven descriptor files being unique relative to the class being introspected. For example, this is true within a jar produced by the {{{http://maven.apache.org/plugins/maven-jar-plugin/}Maven Jar Plugin}}, and also within a jar inside a war. This does not hold within a jar built by {{{http://maven.apache.org/plugins/maven-assembly-plugin/}Maven Assembly Plugin's}} {{{http://maven.apache.org/plugins/maven-assembly-plugin/descriptor-refs.html#jar-with-dependencies}jar-with-dependencies}} descriptor, since there are multiple Maven descriptors relative to a given class, resulting in an ambiguity. * Using project properties To obtain a <<<{{{../apidocs/org/apache/maven/shared/runtime/MavenProjectProperties.html}MavenProjectProperties}}>>> instance for a class's Maven project: --- /** * @component */ private MavenRuntime runtime; public void processProject() throws MavenRuntimeException { MavenProjectProperties project = runtime.getProjectProperties( MyClass.class ); // process project } --- * Using project XML To obtain a <<<{{{http://maven.apache.org/ref/3.0.4/maven-core/apidocs/org/apache/maven/project/MavenProject.html}MavenProject}}>>> instance for a class's Maven project: --- /** * @component */ private MavenRuntime runtime; public void processProject() throws MavenRuntimeException { MavenProject project = runtime.getProject( MyClass.class ); // process project } ---