public void rewriteProject()
        throws MojoExecutionException
    {
        File projectFile = new File( executedProject.getBasedir(), executedProject.getArtifactId() + ".ipr" );

        try
        {
            Document document = readXmlDocument( projectFile, "project.xml" );

            Element module = document.getRootElement();

            // Set the jdk name if set
            if ( jdkName != null )
            {
                setJdkName( module, jdkName );
            }
            else
            {
                String javaVersion = System.getProperty( "java.version" );
                String defaultJdkName;

                if ( ideaVersion.startsWith( "4" ) )
                {
                    defaultJdkName = "java version &quot;" + javaVersion + "&quot;";
                }
                else
                {
                    defaultJdkName = javaVersion.substring( 0, 3 );
                }
                getLog().info( "jdkName is not set, using [java version" + javaVersion + "] as default." );
                setJdkName( module, defaultJdkName );
            }

            setWildcardResourcePatterns( module, wildcardResourcePatterns );

            Element component = findComponent( module, "ProjectModuleManager" );
            Element modules = findElement( component, "modules" );

            removeOldElements( modules, "module" );

            if ( executedProject.getCollectedProjects().size() > 0 )
            {
                Element m = createElement( modules, "module" );
                String projectPath =
                        new File( executedProject.getBasedir(), executedProject.getArtifactId() +"-"+executedProject.getVersion()+ ".iml" ).getAbsolutePath();
                m.addAttribute( "filepath",
                                "$PROJECT_DIR$/" + toRelative( executedProject.getBasedir().getAbsolutePath(),
                                                               projectPath ) );

                for ( Iterator i = executedProject.getCollectedProjects().iterator(); i.hasNext(); )
                {
                    MavenProject p = (MavenProject) i.next();

                    m = createElement( modules, "module" );
                    String modulePath = new File( p.getBasedir(), p.getArtifactId() +"-"+p.getVersion()+ ".iml" ).getAbsolutePath();
                    m.addAttribute( "filepath",
                                    "$PROJECT_DIR$/" + toRelative( executedProject.getBasedir().getAbsolutePath(),
                                                                   modulePath ) );
                }
            }
            else
            {
                Element m = createElement( modules, "module" );
                String modulePath =
                        new File( executedProject.getBasedir(), executedProject.getArtifactId() +"-"+executedProject.getVersion()+ ".iml" ).getAbsolutePath();
                m.addAttribute( "filepath",
                                "$PROJECT_DIR$/" + toRelative( executedProject.getBasedir().getAbsolutePath(),
                                                               modulePath ) );
            }

            // add any PathMacros we've come across
            if ( macros != null && module.elements( "UsedPathMacros" ).size() > 0 )
            {
                Element usedPathMacros = (Element) module.elements( "UsedPathMacros" ).get( 0 );
                removeOldElements( usedPathMacros, "macro" );
                for ( Iterator iterator = macros.iterator(); iterator.hasNext(); )
                {
                    String macro = (String) iterator.next();
                    Element macroElement = createElement( usedPathMacros, "macro" );
                    macroElement.addAttribute( "name", macro );
                }
            }

            writeXmlDocument( projectFile, document );
        }
        catch ( DocumentException e )
        {
            throw new MojoExecutionException( "Error parsing existing IPR file: " + projectFile.getAbsolutePath(), e );
        }
        catch ( IOException e )
        {
            throw new MojoExecutionException( "Error parsing existing IPR file: " + projectFile.getAbsolutePath(), e );
        }
    }