Cet article présente la méthode d'utilisation de Maven pour générer des packages JAR exécutables. Il est partagé avec vous. Les détails sont les suivants:
Du XSI de POM, vous pouvez ouvrir le schéma décrivant POM:
Vous pouvez voir la structure du projet dans le POM:
Le pot généré par l'installation MVN par défaut n'a pas l'entrée de classe principale, et la classe principale doit être définie dans Maven-Compile-Plugin.
<project xmlns = "http://maven.apache.org/pom/4.0.0" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" xsi: schemalation = "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd "> <ModelVersion> 4.0.0 </ Modelversion> <ProupId> com.cetc.di </proupId> <ArtifActid> Hellocetc </pterifactid> </ version> 0.0.1-Snapshot </Derson <name> hellocetc </name> <url> http://maven.apache.org </url> <properties> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> </preperties> <dependencies> <fedency> <groupenid> junit </prepperdid> <artifactid> Junit </ artite> junit </preperdid> <artifactid> Junit <artite> junit </preperdid> <artifactid> Junit <artite> junit </preperdid> <artifactID> JUNDACT </ ARRIFTID> JUNIT </prepperdid> <version> 3.8.1 </ version> <ccope> Test </cope> </ Dependency> </Dependces> <Duild> <Gluginmanagement> <Glugins> <Glugin> <proupId> org.apache.maven.plugins </prouprid> <ErtifactId> Maven-Jar-Plugin </ artifactid> <ponfipture> <source> <Carchive> <Ca Manifest> <Sinclass> com.cetc.di.hellocetc.app </-mainclass> <ddClassPath> true </ addClassPath> <ClassPathPrefix> lib / </callpathprefix> </ manifest> </ archive> </cassdirectory> </smassDirectory> </ pluginManagement> </ build> </ project>
Exécuter l'installation de MVN:
Dans le répertoire cible, il est constaté que le package JAR a été généré:
À l'aide de Java Decompiler, vous pouvez voir que la classe MainClass a été ajoutée au manifeste:
Utilisez une aide MVN: Effective-POM pour voir la structure complète de pom.xml (y compris les propriétés héréditaires):
[Info] scanne pour les projets ... [info] [info] --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Appliqué: <! - ========================================================================. ========================================================================. ========================================================================. ========================================================================. ========================================================================. ========================================================================. ========================================================================. ==========================================================================. ========================================================================. ==========================================================================. ==========================================================================. ==========================================================================. ==========================================================================. ==========================================================================. ==========================================================================. ==========================================================================. ========================================================================================================. xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" xsi: schemalocation = "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <ModelVersion> 4.0.0 </ ModelVersion> <ProupId> com.cetc.di </proncId> <ArtifactId> HELLOCETC </ ArtifactId> <DERNÉRATIV <project.build.sourceencoding> utf-8 </project.build.sourceencoding> </properties> <dépendances> <dependency> <proupId> JUnit </rom groupe> <ArtefactId> JUnit </ artifactId> <Dersond> 3.8.1 </preectory> <scope> </cope> </dependence> <napshots> <veabled> false </abled> </napshots> <id> Central </id> <name> Central Repository </name> <url> https://repo.maven.apache.org/maven2 </url> </popository> </sispository> </maven> </ispository> <DatedatePolicy> Never </ UpdatePolicy> </ releases> <Snapshots> <veabled> false </abled> </napshots> <id> Central </id> <name> Central Repository </name> <url> https://repo.maven.apache.org/maven2 </url> </gluchinrepository> <SourceDirectory> d: / users / a / workspaces / myeclipse 2015 / hellocetc / src / main / java </ sourceDirectory> <scriptsourceDirectory> d: / users / a / workspaces / myeclipse 2015 / hellocetc / src / main / scripts </ sciptsourcedIrctory> <SestSourceDirectory> d: / users / a / workspaces / myeclipse 2015 / hellocetc / src / test / java </ testsourcedIrectory> <utputdirectory> d: / users / a / workspaces / myeclipse 2015 / hellocetc / cible / classes </putdirectory> <testOutputDirectory> d: / users / a / workspaces / myeclipse 2015 / hellocetc / cible / test-classes </ testOutputDirectory> <sessherse> <ourcece> <répertoire> d: / users / a / workspaces / myeclipse 2015 / hellocetc / src / main / raesourcys> </sesshers> </ royal> </sessources> <Arlage> D: / Users / A / Workspaces / MyEclipse 2015 / Hellocetc / SRC / Test / Resources </ Directory> </ TestResources> </ TestResources> <Aldialen> D: / Users / A / Workspaces / MyECLipse 2015 / HelloCetc / Target </ Directory> <inalName> HELLOCETC-0.0.1-SNAPSHO </ FinalName> <Landing Pluginmanagement> <Glugins> <Artifactid> Maven-Antrun-Plugin </ ArfactId> <Dersion> 1.3 </ Version> </Glugin> <Glugin> <RerifactId> Maven-Assembly-Plugin </ ArtifactId> <Dersion> 2.2-beta-5 </ version> </ginin> <plucin> <ArtefactId> Maven-Dependency-Plugin </ Artifactid> <Dersion> 2.8 </DERNIERSE> </GLANGIN> <GLINGIN> <ARFACTID> MAVEN-Release-Plugin </ Artifactid> <version> 2.3.2 </ Version> </ Plugin> <plugin> <Artifactid> Maven-Jar-Jar <fracential> <source> 1.8 </ource> <parget> 1.8 </ target> <carchive> <aMifest> <Anseclass> com.cetc.di.hellocetc.app </-mainclass> <ddClasspath> true </ddclasspath> <classpathprefix> lib / </cscyspathprefix> </crasspathprefix> lib / </ classpathprefix> </cathPest> </ Configuration> </ Plugin> </ Plugins> </ Plugins> </ PluginManagement> <Glugins> <ArtifActid> Maven-Clean-Plugin </ ArfactId> <De version> 2.5 </ Version> <Executions> <Exécution> Clean </ Organims> </id> </ EXECUTION> </pasitions> <but </gingin> <Glugin> <Artifactid> Maven-Resources-Plugin </ Arfactive> <DERVIÈRE> 2.6 </ Version> <Cecultes> <Dexecution> <Id> Default-TestResources </id> <gase> Process-Test-Resources </ Phase> <Coffés> </ Id> TestResources </puil> </purés> </Exécution> </ buts> </ exécution> <exécution> <id> par défaut-resources </id> <gasup> Process-resources </ phase> <objectifs> <buty> </ but> </Exécution> </gingin> <plugin> <pilaCtid> maven-jar-plagin </ artifactid> <version> 2.4 </Dection> <Executions> <execution> <gase> package </gase> <tobjectifs> <tobjectif> jar </ but> </ buts> <fonfi configuration> <source> 1.8 </ source> <Barget> 1.8 </ Target> <Carchive> <Affeste> <Mainclass> com.cetc.di.hellocetc.App </-mainclass> <dddClasspath> true </dddclasspath> <ClassPathPrefix> lib / </ classpathprefix> </ manifeste> </ archive> <classDirectory /> </ configuration> </ exécution> </ exécution> <fonfi configuration> <source> 1.8 </source> <parget> 1.8 </ Target> <Carchive> <Manifest> <MainClass> com.cetc.di.Hellocetc.app.app.appr </-mainclass> com.cetc.di.Hellocetc.app.app.app.Appass> COM.CETC.DI.HELLOCETC. <ddClassPath> true </ addClassPath> <ClassPathPrefix> lib / </sclasspathprefix> </ manifeste> </ archive> <classesdirectory /> </ configuration> </ plugin> <plugin> <ErtifactId> Maven-Compiler-Plugin </ ArtifactId> <version> 3.1 </ version> <id> Default-Compile </id> <gasup> compiler </ phase> <objectifs> <in but> Compiler </pilé> </ buts> </ EXECUTION> </ EXECUTION> <ID> Default-TestCompile </id> <passine> Test-Compile </ Eécution> <ArtefactId> Maven-Surefire-Plugin </ Arfactive> <DERNEMENT> 2.12.4 </ Version> <Executions> <Execution> <Exécution> <Id> TEST DEFAULT </ ID> </ EXÉCUTION> TEST </ EXÉCLITIONS> </ PLUS> TEST </ but> </ buts> </kétion> </EXECTIONS> </ Plugin> </ Plugin> <artifactId>maven-install-plugin</artifactId> <version>2.4</version> <executions> <execution> <id>default-install</id> <phase>install</phase> <goals> <goal>install</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version> 2.7 </ version> <Executions> <Deamution> <id> Default-Deploy </id> <gase> Deploy </ phase> <butt> <butt> Deploy </ but> </pjectif> </ exécution> </ exécution> </glugin> <plugin> <eRtifactid> Maven-Site-Plugin </ ArtifactId> <Dersion> 3.3 </ Version> <Id> Site par défaut </id> <gase> Site </ phase> <pose> <tobjectif> Site </ but> </ings> <filony> <putputdirectory> d: / users / a / workspaces / myeclipse 2015 / hellocetc / target / site </ outputDirectory> <peportsplugins> <peplugin> <proprid> org.apache.maven.plugins </prougin> <ArtefactId> Maven-Project-Info-Reports-Plugin </ ArfactId> </ReportPlugin> </ReportPlugins> </ ReportPlugins> </ Configuration> </ EXECUTION> <DEXUCUTION> <ID> Default-Deploy </id> <matheguration> Site-Deleploy </strys> <butor> Deploy </pual> <OutputDirectory> d: / users / a / workspaces / myeclipse 2015 / hellocetc / target / site </putputdirectory> <pepleplugins> <peportplugin> <proupId> org.apache.maven.plugins </proncId> <Artifactid> MavenPlugin> </portplugins> </portplugins> </ configuration> </ exécution> </Executions> <Iconguration> <putputdirectory> d: / users / a / workspaces / myeclipse 2015 / hellocetc / ciblerd> Org.apache.maven. <ArtefactId> Maven-Project-Info-Reports-Plugin </ ArfactId> </ReportPlugin> </ ReportPlugins> </ Configuration> </ Plugin> </ Plugins> </Duild> <Deports> <OutputDirectory> D: / Userst / A / Workspaces / MyEclips 2015 / Hellocetc / Target / Site </ OutputDirctory> </plating> </ project> [info] ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 2015-11-18T20: 05: 12 + 08: 00 [info] Mémoire finale: 10m / 245m [info] -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Ce qui précède est tout le contenu de cet article. J'espère que cela sera utile à l'apprentissage de tous et j'espère que tout le monde soutiendra davantage Wulin.com.