Write custom archetype with eclipse

archetype: translated into prototype, which is actually what we call scaffolding.

Maven has provided us with many archetypes, but many times we still need to manually make our own personalized archetypes.

 

1. Make a maven project (model-project) (including tool classes, configuration files, etc.), our goal is to generate the look of this project through a custom archetype.

2. Right-click the project model-project run as and run the command: archetype:create-from-project, which will generate the /target/generated-sources/archetype code.

3. Create a new ordinary java project template, right-click configure--->conver to maven, fill in the maven coordinates, delete the src directory , and back up pom.xml.

4. Copy the code under /target/generated-sources/archetype generated in step 2 to the template project, overwrite the original code, modify the coordinates in pom. The SNAPSHOT is removed to ensure that the archetype can be seen later if it is not a snapshot.

5. Install template project.

6. When creating a new project here, you can select the archetype we just defined in the default local.

 

Note: Sometimes you need to modify the file /src/main/resources/META-INF/maven/archetype-metadata.xml

filtered: Whether to filter, such as replacing the package name and coordinates in the class with the coordinates and package name entered by the user.

packaged: Whether to output by package, for example, src/main/java will cascade to generate three directories.

The general structure of the file is:

 

<?xml version="1.0" encoding="UTF-8"?>
<archetype-descriptor xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0 http://maven.apache.org/xsd/archetype-descriptor-1.0.0.xsd" name="hello-api"
    xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <fileSets>
    <fileSet filtered="true" packaged="true" encoding="UTF-8">
      <directory>src/main/java</directory>
      <includes>
        <include>**/*.*</include>
      </includes>
    </fileSet>
    <fileSet filtered="true" packaged="flase" encoding="UTF-8">
      <directory>src/main/php</directory>
      <includes>
        <include>**/*.*</include>
      </includes>
    </fileSet>
 </fileSets>   
</archetype-descriptor>

 

It is easy to understand, but there is a special case where the parent project structure is as follows:

 

<modules>
		<module dir="api" name="service-api">
			<fileSets>
				<fileSet filtered="true" encoding="UTF-8" packaged="true">
					<directory>src/main/java</directory>
					<includes>
						<include>**/*.*</include>
					</includes>
				</fileSet>
				<fileSet filtered="true" encoding="UTF-8" packaged="true">
					<directory>src/test/java</directory>
					<includes>
						<include>**/*.*</include>
					</includes>
				</fileSet>
				<fileSet filtered="true" encoding="UTF-8" packaged="false">
					<directory>src/main/php</directory>
					<includes>
						<include>**/*.*</include>
					</includes>
				</fileSet>
				<fileSet filtered="true" encoding="UTF-8" packaged="false">
					<directory>src/main/csharp</directory>
					<includes>
						<include>**/*.*</include>
					</includes>
				</fileSet>
				<fileSet filtered="true" encoding="UTF-8" packaged="false">
					<directory>src/main/html</directory>
					<includes>
						<include>**/*.*</include>
					</includes>
				</fileSet>
				<fileSet filtered="true" encoding="UTF-8" packaged="false">
					<directory>src/main/resources</directory>
					<includes>
						<include>**/*.*</include>
					</includes>
				</fileSet>
				<fileSet filtered="true" encoding="UTF-8" packaged="false">
					<directory>src/test/resources</directory>
					<includes>
						<include>**/*.*</include>
					</includes>
				</fileSet>
			</fileSets>
		</module>
		<module dir="service" name="service-service">
			<fileSets>
				<fileSet filtered="true" encoding="UTF-8" packaged="false">
					<directory>META-INF/service-conf</directory>
					<includes>
						<include>**/*.*</include>
					</includes>
				</fileSet>
				<fileSet filtered="true" encoding="UTF-8" packaged="true">
					<directory>src/main/java</directory>
					<includes>
						<include>**/*.*</include>
					</includes>
				</fileSet>
				<fileSet filtered="true" encoding="UTF-8" packaged="true">
					<directory>src/test/java</directory>
					<includes>
						<include>**/*.*</include>
					</includes>
				</fileSet>
				<fileSet filtered="true" encoding="UTF-8" packaged="false">
					<directory>src/main/resources</directory>
					<includes>
						<include>**/*.*</include>
					</includes>
				</fileSet>
				<fileSet filtered="true" encoding="UTF-8" packaged="false">
					<directory>src/test/resources</directory>
					<includes>
						<include>**/*.*</include>
					</includes>
				</fileSet>
			</fileSets>
		</module>
	</modules>

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327052801&siteId=291194637