Building Web Applications with Maven 2[转自官网]

官网:http://today.java.net/pub/a/today/2007/03/01/building-web-applications-with-maven-2.html

 

You may have heard of Maven 2--it's often touted by
technologists as a replacement for Ant. You may have even taken
some time to browse around on the Maven 2 site , but
maybe the documentation has left you a little bit unclear on where and
how to go about getting started.

In this article, we will take a look at using Maven 2 to help
build a simple web application (a bit of business logic in a JAR
and a JSP-based web application). By the end of this article, you
should feel comfortable working with Maven 2, and ready to start
using it as a much more satisfactory tool than Ant (or even your
IDE).

Getting Started

These instructions assume that you have installed Java 5 and Maven
2
. The following two commands shown should work at your command
line:

C

:

\&gt

;

java 

-

version


 java version 

"1.5.0_06"


 

Java

(

TM

)

 

2

 

Runtime

 

Environment

,

 

Standard

 

Edition

 

(

build 

1.5

.

0_06

-

b05

)


 

Java

 

HotSpot

(

TM

)

 

Client

 VM 

(

build 

1.5

.

0_06

-

b05

,

 mixed mode

,

 sharing

)



C

:

\&gt

;

mvn 

-

v


Maven

 version

:

 

2.0

.

5



Everything else required for this project will be downloaded for
you automatically by Maven 2 (obviously, a working internet
connection is also required). I used my Windows system to write
this article, but everything here should work fine on Mac OS X,
Linux, Solaris, etc.

From a high level, the project will be organized into two
subprojects (one for the JAR and one for the WAR). Let's start by
creating the base directory for the project. This directory serves
as the base for the other folders.

C

:

\&gt

;

mkdir maven2example


C

:

\&gt

;

cd maven2example


C

:

\maven2example

&

gt

;



Now, let's create the two subprojects. Maven 2 supports the
notion of creating a complete project template with a simple
command. The project templates (called “archetypes” in
Maven) shown below are a subset of the full list of archetypes built in to Maven 2.

Project Template (Archetype)

Purpose

maven-archetype-archetype

Create your own project template (archetype).

maven-archetype-j2ee-simple

Creates a J2EE project (EAR), with directories and subprojects
for the EJBs, servlets, etc.

maven-archetype-mojo

Create your own Maven 2 plugins.

maven-archetype-quickstart

Simple Java project, suitable for JAR generation. Maven 2
default.

maven-archetype-site

Documentation-only site, with examples in several formats. You
can run this archetype on top of an existing Maven 2 project to add
integrated documentation.

maven-archetype-webapp

Creates a web application project (WAR), with a simple Hello
World JSP.

These archetypes are analogous to the sample projects you might
find in your IDE as defaults for standard "New
Project…" options.

To create a simple Java project, you simply execute the command
as shown.

mvn archetype

:

create


   

-

DgroupId

=[

your project

's group id]


   -DartifactId=[your project'

s artifact id

]



The mvn command invokes the Maven 2 system, in this
case a request to run the archetype plugin with the
create command. The - D commands are simply setting
Java system properties, thereby passing configuration information
to Maven 2. Per the Maven 2 FAQ, the groupID should
follow the package name (reversed DNS of your website), and can
contain subgroups as appropriate. For example, the sample code for
my books might use com . cascadetg . hibernate or
com . cascadetg . macosx . In this case, let's use the
com . attainware . maven2example .

The artifactID is specific to each artifact and by
convention should be the filename, excluding extension. In this
case, we would like to create two artifacts, a JAR file containing
the logic and a WAR file containing the web application. First,
let's create the JAR file using the command as shown below. Maven 2
tends to be quite verbose, and so I have trimmed the output shown
here and and many of the other listings in this article to focus on
the elements of interest.

<b>

C:\maven2example&gt;mvn archetype:create


       -DgroupId=com.attainware.maven2example


     -DartifactId=maven2example_logic



</b>

[INFO]   Scanning for projects...


[INFO] Searching repository for plugin with prefix: 'archetype'.


[INFO]   -----------------------------------------------------


[INFO] Building Maven Default Project


   

<i>

&lt;snip&gt;


</i>

[INFO] Archetype created in dir:   


                        C:\maven2example\maven2example_logic


[INFO]   -----------------------------------------------------


[INFO] BUILD SUCCESSFUL


[INFO]   -----------------------------------------------------


[INFO] Total time: 1 second


[INFO] Finished at: Sun Feb 04 10:42:33 PST 2007


[INFO] Final Memory: 4M/8M


[INFO]   -----------------------------------------------------



C:\maven2example&gt;



Looking at the resulting file structure, we can see that Maven 2
has done several things for us. It's created a fairly complete
directory structure, following many best practices for organizing
code. The source code is broken into two directories, one for the
code itself and one for the test cases. The package structure is
taken from the groupId , and is mirrored in both the
main code and the test case directory structure.

Source for JAR Project Layout
Figure 1. Source for JAR project layout

Clicking through the results, you will see folders and two Java
source files--so far, nothing surprising.

In the root folder, however, we notice a file called pom.xml .

Maven 2 Project File for JAR Project
Figure 2. Maven 2 project file for JAR project

This file is essentially analogous to an IDE project file: it
contains all of the information about a project, and is the file
that Maven 2 uses to act upon to execute commands. The contents of
pom.xml :

&

lt

;

project xmlns

=

 

http

:

//maven.apache.org/POM/4.0.0




      xmlns

:

xsi

=

"http://www.w3.org/2001/XMLSchema-instance"


        xsi

:

schemaLocation

=

"http://maven.apache.org/POM/4.0.0   


       

http://maven.apache.org/maven-v4_0_0.xsd"

&

gt

;




     

&

lt

;

modelVersion

&

gt

;

4.0

.

0

&

lt

;

/modelVersion&gt;


     &lt;groupId&gt;com.attainware.maven2example&lt;/

groupId

&

gt

;


        

&

lt

;

artifactId

&

gt

;

maven2example_logic

&

lt

;

/artifactId&gt;


     &lt;packaging&gt;jar&lt;/

packaging

&

gt

;


     

&

lt

;

version

&

gt

;

1.0

-

SNAPSHOT

&

lt

;

/version&gt;


     &lt;name&gt;maven2example_logic&lt;/

name

&

gt

;


     

&

lt

;

url

&

gt

;

http

:

//maven.apache.org&lt;/url&gt;


     

&

lt

;

dependencies

&

gt

;


       

&

lt

;

dependency

&

gt

;


         

&

lt

;

groupId

&

gt

;

junit

&

lt

;

/groupId&gt;


         &lt;artifactId&gt;junit&lt;/

artifactId

&

gt

;


           

&

lt

;

version

&

gt

;

3.8

.

1

&

lt

;

/version&gt;


         &lt;scope&gt;test&lt;/

scope

&

gt

;


       

&

lt

;

/dependency&gt;


     &lt;/

dependencies

&

gt

;


 

&

lt

;/

project

&

gt

;



There really isn't a lot in the standard pom.xml file. We
can see the groupId and artifactId , that
the project is intended to build a JAR, and that the current
version is to 1.0-SNAPSHOT. The project uses JUnit 3.8.1 for unit
tests. We could change the URL to point to our company or project
website and/or update the version number to something more
appropriate, but for now this is fine. If you are curious, you can
review the schema for Maven pom.xml files.

Maven 2 makes heavy use of standard directory layouts to reduce
clutter. It assumes that the source code and test code will be
found in the directory created by the project template. By
specifying standard directory layouts , it easier to immediately start work on a project with spending a lot of time relearning the build
process. While it is possible to reconfigure Maven 2 to use custom
directory layouts, I have found that it's generally less work to
simply use the Maven 2 layout. I have converted several projects of
small to medium size from Ant to Maven 2, and found that the size
and complexity of my build files (from custom Ant build.xml
files to Maven 2 pom.xml files) dropped by an order of magnitude.
By using Maven 2 templates (archetypes) and simply copying source
files into the proper locations, I found that I wound up with much
more comprehensible project structures than the slowly accreted,
custom Ant build.xml projects. While it is possible to force Maven
2 to fit into arbitrary directory structures, that's probably not
the place to start.

Maven 2 Commands

Maven 2 supports two kinds of commands that can be run on
projects (pom.xml files). The first type of command is a
plugin command. Plugin commands include things like "copy a
set of files," "compile a source tree," etc. The other type of
command is a lifecycle command. A lifecycle command is a
series of plugin commands strung together. For example, the test
lifecycle command might include several plugin commands in a
series.

Let's execute the lifecycle command mvn
test on our pom.xml file. As can
be seen in the following listing, this command executes several
plugins (additional output omitted for readability).

C

:

\maven2example\maven2example_logic

&

gt

;

mvn test



[

INFO

]

 

Scanning

 

for

 projects

...


[

INFO

]

   

-------------------------------------------------------


[

INFO

]

 

Building

 maven2example_logic


[

INFO

]

    task

-

segment

:

 

[

test

]


[

INFO

]

   

-------------------------------------------------------


<b>

[

INFO

]

 

[

resources

:

resources

]


[

INFO

]

 

[

compiler

:

compile

]


[

INFO

]

 

[

resources

:

testResources

]


[

INFO

]

 

[

compiler

:

testCompile

]


[

INFO

]

 

[

surefire

:

test

]


</

b

>[

INFO

]

   

------------------------------------------------------


[

INFO

]

 BUILD SUCCESSFUL



C

:

\maven2example\maven2example_logic

&

gt

;



What we can see is that the mvn test lifecycle
command is bound to several plugins, including
resources , compiler , and
surefire . These plugins in turn are called with
different goals, such as compile or
testCompile . So, by simply calling mvn
with a single lifecycle command, we execute a number of plugins--no additional configuration necessary. If you want to specify a
plugin directly, that's fine. For example:

C

:

\maven2example\maven2example_logic

&

gt

;

mvn compiler

:

compile



[

INFO

]

 

Scanning

 

for

 projects

...


[

INFO

]

 

Searching

 repository 

for

 plugin 

with

 prefix

:

 

'compiler'

.


[

INFO

]

 

--------------------------------------------------------


[

INFO

]

 

Building

 maven2example_logic


[

INFO

]

    task

-

segment

:

 

[

compiler

:

compile

]


[

INFO

]

 

--------------------------------------------------------


[

INFO

]

 

[

compiler

:

compile

]


[

INFO

]

 

Nothing

 to compile 

-

 all classes are up to date


[

INFO

]

 

--------------------------------------------------------


[

INFO

]

 BUILD SUCCESSFUL


[

INFO

]

 

--------------------------------------------------------



C

:

\maven2example\maven2example_logic

&

gt

;



Now that we have seen both lifecycle commands and plugin
commands in action, let's look at some of the typical lifecycle
commands.

mvn clean

Cleans out all Maven-2-generated files.

mvn compile

Compiles Java sources.

mvn test-compile

Compiles JUnit test classes.

mvn test

Runs all JUnit tests in the project.

mvn package

Builds the JAR or WAR file for the project.

mvn install

Installs the JAR or WAR file in the local Maven repository (use
this if you have multiple interdependent local projects).

A complete list of lifecycle commands can be found at on the Maven 2 site.

Similarly, here is a list of some of the more popular plugin
commands, analogous to the popular tasks in Ant or commands in an
IDE. Note that a lifecycle command name is a single word, whereas
a plugin command name is broken into a plugin and a specific goal
with a colon:

clean:clean

Cleans up after the build.

compiler:compile

Compiles Java sources.

surefire:test

Runs the JUnit tests in an isolated classloader.

jar:jar

Creates a JAR file.

eclipse:eclipse

Generates an Eclipse project file from the pom.xml file.

You may want to take some time perusing the list of plugins
that are available to Maven 2 by default. It is possible to add
additional plugins by either writing them yourself or by grabbing
them from a repository (repositories are discussed later in this
article).

You can configure plugins on a per-project basis by updating
your pom.xml file. For example, if you wish to force compilation
on Java 5, simply pass in the following option under the build
configuration in the pom.xml file:

&

lt

;

project xmlns

=

"http://maven.apache.org/POM/4.0.0"

 


xmlns

:

xsi

=

  

"http://www.w3.org/2001/XMLSchema-instance"

 


xsi

:

schemaLocation

=

  

"http://maven.apache.org/POM/4.0.0   


http://maven.apache.org/maven-v4_0_0.xsd"

&

gt

;


       

&

lt

;

build

&

gt

;


         

&

lt

;

plugins

&

gt

;


           

&

lt

;

plugin

&

gt

;


              

&

lt

;

groupId

&

gt

;

org

.

apache

.

maven

.

plugins

&

lt

;

/groupId&gt;


              &lt;artifactId&gt;maven-compiler-plugin&lt;/

artifactId

&

gt

;


              

&

lt

;

configuration

&

gt

;


                   

&

lt

;

source

&

gt

;

1.5

&

lt

;

/source&gt;


                   &lt;target&gt;1.5&lt;/

target

&

gt

;


              

&

lt

;

/configuration&gt;


           &lt;/

plugin

&

gt

;


         

&

lt

;

/plugins&gt;


       &lt;/

build

&

gt

;


 

&

lt

;/

project

&

gt

;



Each plugin listed on the Maven 2 site includes documentation on the various configuration options available.

Maven 2 Versus Ant?

If you are familiar with Ant, you may be wondering why
lifecycles and plugins are an improvement over targets and tasks.
While you can cleanly separate your Ant targets from the various
directories and artifacts, it's surprisingly difficult to do. For
example, let's say that you have several directories, with several
artifacts (multiple JAR files, which get combined into a WAR, for
example). Every command that you write for each Ant target will
require properties for the source, and various dependencies,
typically passed around via a combination of Ant variables.
Dependencies in particular require a lot of custom handholding,
with JAR files downloaded and managed by hand.

Maven 2, on the other hand, assumes that given a pom.xml file, you
are probably always going to be doing the same general kind of
things with your code. You are probably going to want to build a
JAR file from that lump of code over there . You are probably
going to want to build a WAR file from that set of files over
there . So those actions (or goals) are written in Java as
reusable chunks of code (plugins). Instead of complicated lumps of
thousands of lines of custom build.xml files, you just call a
plugin with a specific goal.

There are several advantages to this approach. For one, the
Maven 2 Java-based plugins are a lot smarter than the lower-level
commands found in Ant tasks. You don't have to write custom tasks
that handle compilation, and then have to worry about keeping your
clean task in sync with your compile and packaging tasks; Maven 2
just takes care of this stuff for you.

Another advantage of this model is that it encourages reuse of
plugins in a much more robust way than Ant tasks. Later in this
article, we will show how simply adding a few lines to a pom.xml
file will automatically download and launch Jetty (a lightweight
servlet/JSP container), seamlessly installing and running the WAR
file.

Adding Some Business Logic

Let's add a tiny bit of pseudo-business logic to our
application. First, we write a simple test cast that looks for a
String, updating C:\maven2example\maven2example_logic\src\test\java\com\attainware\maven2example\AppTest.java .

 

package

 com

.

attainware

.

maven2example

;


 

import

 junit

.

framework

.

Test

;


 

import

 junit

.

framework

.

TestCase

;


 

import

 junit

.

framework

.

TestSuite

;


 

import

 com

.

attainware

.

maven2example

.

App

;


 


 

public

 

class

 

AppTest

 

extends

 

TestCase


 

{


       

public

 

void

 testApp

()

  


       

{


           assertTrue

(

 

App

.

now

().

length

()

 

&

gt

;

 

0

 

);


       

}


 

}



Opening the file
C:\maven2example\maven2example_logic\src\main\java\com\attainware\maven2example\App.java ,
we update the contents to:

package

 com

.

attainware

.

maven2example

;



public

 

class

 

App


{


       

public

 

static

 

String

 now

()


       

{


            

return

 

new

 java

.

util

.

Date

().

toString

();

  


       

}


 

}



Running the command mvn install , as shown below,
causes several plugins to run (status messages omitted for
readability).

 C

:

\maven2example\maven2example_logic

&

gt

;

mvn install



 

[

INFO

]

 

[

resources

:

resources

]


 

[

INFO

]

 

[

compiler

:

compile

]


 

[

INFO

]

 

[

resources

:

testResources

]


 

[

INFO

]

 

[

compiler

:

testCompile

]


 

[

INFO

]

 

[

surefire

:

test

]


 

[

INFO

]

 

[

jar

:

jar

]


 

[

INFO

]

 

[

install

:

install

]


 

[

INFO

]

 BUILD SUCCESSFUL



 C

:

\maven2example\maven2example_logic

&

gt

;



By running the mvn install lifecycle command, Maven
2 finishes by installing the JAR file into the local repository,
making this resulting JAR file available to other projects on this
system. This allows this JAR file to be available for inclusion in
our WAR file.

Creating the WAR file

Now that we have created our JAR file, creating the WAR file is
straightforward--we just pass in a different archetype ID.

C

:

\maven2example

&

gt

;<

b

>

mvn archetype

:

create


   

-

DgroupId

=

com

.

attainware

.

maven2example


   

-

DartifactId

=

maven2example_webapp


   

-

DarchetypeArtifactId

=

maven

-

archetype

-

webapp

</

b

>



[

INFO

]

 

Scanning

 

for

 projects

...


[

INFO

]

 

Searching

 repository 

for

 plugin 

with

 prefix

:

 

'archetype'

.


[

INFO

]

 

---------------------------------------------------------


[

INFO

]

 

Building

 

Maven

 

Default

 

Project


[

INFO

]

 

[

archetype

:

create

]


[

INFO

]

 

Archetype

 created 

in

 dir

:

 


                        C

:

\maven2example\maven2example_webapp


[

INFO

]

 

---------------------------------------------------------


[

INFO

]

 BUILD SUCCESSFUL


[

INFO

]

 

---------------------------------------------------------



C

:

\maven2example

&

gt

;



As shown in Figure 3, we now have a directory structure you
would expect to see for a WAR. Note that the JAR file is
automatically pulled into the WAR file from the local
repository.

WAR Project Directory Structure
Figure 3. WAR project directory structure

Simply executing the command mvn package creates
our WAR file (again, omitting most of the status messages besides
the plugin list).

In this case, the mvn package command builds the
WAR file but does not install it into the local repository
(repositories will be discussed later). As you build larger, more
sophisticated projects with more complex dependencies, you may wish
to be more deliberate about using the mvn package and
mvn install commands to control inter-project
dependencies. Because the WAR file is the final artifact we are
creating, we can save a few compute cycles merely by packaging the
WAR file in the local target directory.

 C

:

\maven2example\maven2example_webapp

&

gt

;

mvn 

package



 

[

INFO

]

 

Scanning

 

for

 projects

...


 

[

INFO

]

 

--------------------------------------------------------


 

[

INFO

]

 

Building

 maven2example_webapp 

Maven

 

Webapp


 

[

INFO

]

    task

-

segment

:

 

[

package

]


 

[

INFO

]

 

--------------------------------------------------------


 

[

INFO

]

 

[

resources

:

resources

]


 

[

INFO

]

 

[

compiler

:

compile

]


 

[

INFO

]

 

[

resources

:

testResources

]


 

[

INFO

]

 

[

compiler

:

testCompile

]


 

[

INFO

]

 

[

surefire

:

test

]


 

[

INFO

]

 

[

war

:

war

]


 

[

INFO

]

 

Building

 war

:

 


         C

:

\maven2example\maven2example_webapp\


             target\maven2example_webapp

.

war


 

[

INFO

]

 

-------------------------------------------------------


 

[

INFO

]

 BUILD SUCCESSFUL


 

[

INFO

]

 

-------------------------------------------------------



 C

:

\maven2example\maven2example_webapp

&

gt

;



As we can see in Figure 4, we now have a WAR file.

Resulting WAR File
Figure 4. Resulting WAR file

Now that we have a WAR file, we would like to be able to run the
application. To do this, we simply add the Jetty plugin to the
pom.xml for this web application as shown:

&

lt

;

project xmlns

=

  

"http://maven.apache.org/POM/4.0.0"


   xmlns

:

xsi

=

  

"http://www.w3.org/2001/XMLSchema-instance"


  xsi

:

schemaLocation

=

  

"http://maven.apache.org/POM/4.0.0   


  

http://maven.apache.org/maven-v4_0_0.xsd"

&

gt

;




     

&

lt

;

modelVersion

&

gt

;

4.0

.

0

&

lt

;

/modelVersion&gt;


     &lt;groupId&gt;com.attainware.maven2example&lt;/

groupId

&

gt

;


     

&

lt

;

artifactId

&

gt

;

maven2example_webapp

&

lt

;

/artifactId&gt;


     &lt;packaging&gt;war&lt;/

packaging

&

gt

;


     

&

lt

;

version

&

gt

;

1.0

-

SNAPSHOT

&

lt

;

/version&gt;


     &lt;name&gt;maven2example_webapp   Maven Webapp&lt;/

name

&

gt

;


     

&

lt

;

url

&

gt

;

http

:

//maven.apache.org&lt;/url&gt;


     

&

lt

;

dependencies

&

gt

;


       

&

lt

;

dependency

&

gt

;


         

&

lt

;

groupId

&

gt

;

junit

&

lt

;

/groupId&gt;


         &lt;artifactId&gt;junit&lt;/

artifactId

&

gt

;


           

&

lt

;

version

&

gt

;

3.8

.

1

&

lt

;

/version&gt;


         &lt;scope&gt;test&lt;/

scope

&

gt

;


       

&

lt

;

/dependency&gt;


     &lt;/

dependencies

&

gt

;


     

&

lt

;

build

&

gt

;


       

&

lt

;

finalName

&

gt

;

maven2example_webapp

&

lt

;

/finalName&gt;


         <b>&lt;plugins&gt;


             &lt;plugin&gt;


                  &lt;groupId&gt;org.mortbay.jetty&lt;/

groupId

&

gt

;


                  

&

lt

;

artifactId

&

gt

;

maven

-

jetty

-

plugin

&

lt

;

/artifactId&gt;


             &lt;/

plugin

&

gt

;


         

&

lt

;

/plugins&gt;


</

b

>

   

&

lt

;

/build&gt;


 &lt;/

project

&

gt

;



By adding the plugins entry with the Jetty information, we can
now run our web application by simply typing the command as shown
below.

 C

:

\maven2example\maven2example_webapp

&

gt

;<

b

>

mvn jetty

:

run


</

b

>


 

[

INFO

]

 

Scanning

 

for

 projects

...


 

[

INFO

]

 

Searching

 repository 

for

 plugin 

with

 prefix

:

 

'jetty'

.


 

[

INFO

]

   

----------------------------------------------------


 

[

INFO

]

 

Building

 maven2example_webapp 

Maven

 

Webapp


 

[

INFO

]

    task

-

segment

:

   

[

jetty

:

run

]


 

[

INFO

]

   

----------------------------------------------------


 

[

INFO

]

 

Preparing

 jetty

:

run


 

[

INFO

]

 

[

resources

:

resources

]


 

[

INFO

]

 

[

compiler

:

compile

]


 

[

INFO

]

 

[

jetty

:

run

]


 

[

INFO

]

 

Starting

 jetty 

6.1

.

0pre0

   

...


 

[

INFO

]

 

Classpath

 

=

   


   

[

file

:

/C:/

maven2example

/

maven2example_webapp

/

target

/

classes

/]


   

2007

-

02

-

04

 

12

:

41

:

24.015

::

INFO

:


    

Started

 

SelectChannelConnector

 

@

   

0.0

.

0.0

:

8080


 

[

INFO

]

 

Started

 

Jetty

 

Server



We can now open our web browser to localhost:8080/maven2example_webapp
to see our web application (as shown in Figure 5). Type
Ctrl -C in the console window to shut down the
server. Note that we didn't have to download and install Jetty
separately--Maven 2 automatically downloads and configures
Jetty.

Hello World Web Application
Figure 5. Hello World Web Application

Tying Together the Logic and Web Application

Now, we simply have to tie together the logic in our JAR file
and the web application. First, we want to set up a dependency
between our JAR and the web application. This will tell Maven 2
that we want to use this JAR file in our WAR, which will cause
Maven 2 to automatically copy the JAR file when we package our WAR
file.

First, we add the dependency on this other JAR file to our
pom.xml for the web application as shown below.

&

lt

;

project xmlns

=

  

"http://maven.apache.org/POM/4.0.0"


 xmlns

:

xsi

=

  

"http://www.w3.org/2001/XMLSchema-instance"

 


 xsi

:

schemaLocation

=

  

"http://maven.apache.org/POM/4.0.0   


 

http://maven.apache.org/maven-v4_0_0.xsd"

&

gt

;




     

&

lt

;

modelVersion

&

gt

;

4.0

.

0

&

lt

;

/modelVersion&gt;


     &lt;groupId&gt;com.attainware.maven2example&lt;/

groupId

&

gt

;


     

&

lt

;

artifactId

&

gt

;

maven2example_webapp

&

lt

;

/artifactId&gt;


     &lt;packaging&gt;war&lt;/

packaging

&

gt

;


     

&

lt

;

version

&

gt

;

1.0

-

SNAPSHOT

&

lt

;

/version&gt;


     &lt;name&gt;maven2example_webapp   Maven Webapp&lt;/

name

&

gt

;


     

&

lt

;

url

&

gt

;

http

:

//maven.apache.org&lt;/url&gt;


     

&

lt

;

dependencies

&

gt

;


       

&

lt

;

dependency

&

gt

;


         

&

lt

;

groupId

&

gt

;

junit

&

lt

;

/groupId&gt;


         &lt;artifactId&gt;junit&lt;/

artifactId

&

gt

;


           

&

lt

;

version

&

gt

;

3.8

.

1

&

lt

;

/version&gt;


         &lt;scope&gt;test&lt;/

scope

&

gt

;


       

&

lt

;

/dependency&gt;


   <b>    &lt;dependency&gt;


           &lt;groupId&gt;com.attainware.maven2example&lt;/

groupId

&

gt

;


            

&

lt

;

artifactId

&

gt

;

maven2example_logic

&

lt

;

/artifactId&gt;


           &lt;version&gt;1.0-SNAPSHOT&lt;/

version

&

gt

;


       

&

lt

;

/dependency&gt;


</

b

>

     

&

lt

;

/dependencies&gt;


     &lt;build&gt;


       &lt;finalName&gt;maven2example_webapp&lt;/

finalName

&

gt

;


         

&

lt

;

plugins

&

gt

;


              

&

lt

;

plugin

&

gt

;


                

&

lt

;

groupId

&

gt

;

org

.

mortbay

.

jetty

&

lt

;

/groupId&gt;


                &lt;artifactId&gt;maven-jetty-plugin&lt;/

artifactId

&

gt

;


              

&

lt

;

/plugin&gt;


         &lt;/

plugins

&

gt

;


     

&

lt

;

/build&gt;


 &lt;/

project

&

gt

;



We then update our JSP file to use our fancy new business
logic.

&

lt

;

html

&

gt

;


 

&

lt

;

body

&

gt

;


 

&

lt

;

h2

&

gt

;

Fancy

 

Clock

&

lt

;

/h2&gt;


 &lt;%= com.attainware.maven2example.App.now() %&gt;


 &lt;/

body

&

gt

;


&

lt

;/

html

&

gt

;



Next, we run the command mvn package to rebuild the
WAR file.

 C

:

\maven2example\maven2example_webapp

&

gt

;

mvn 

package



 

[

INFO

]

 

Scanning

 

for

 projects

...


 

[

INFO

]

   

---------------------------------------------------


 

[

INFO

]

 

Building

 maven2example_webapp 

Maven

 

Webapp


 

[

INFO

]

    task

-

segment

:

   

[

package

]


 

[

INFO

]

   

---------------------------------------------------


 

[

INFO

]

 

[

resources

:

resources

]


 

[

INFO

]

 

[

compiler

:

compile

]


 

[

INFO

]

 

[

resources

:

testResources

]


 

[

INFO

]

 

[

compiler

:

testCompile

]


 

[

INFO

]

 

[

surefire

:

test

]


 

[

INFO

]

 

[

war

:

war

]


 

[

INFO

]

 

Building

 war

:

   


     C

:

\maven2example\maven2example_webapp\


          target\maven2example_webapp

.

war


 

[

INFO

]

   

---------------------------------------------------


 

[

INFO

]

 BUILD SUCCESSFUL


 

[

INFO

]

 

-----------------------------------------------------



Notice that the JAR file is now copied into WAR file, as shown
in Figure 6.

Verifying the JAR copied into the WAR
Figure 6. Verifying the JAR copied into the WAR

When we execute the mvn jetty : run command and view
the results in our browser, we see the results as shown in Figure
7.

WAR running with Business Logic from JAR
Figure 7. WAR running with business logic from JAR

To recap, we now have two independent projects running on our
system. When we run mvn install on the logic, the JAR
file installed in the local repository will be updated. When we run
mvn package on the web application, it will pick up
the latest copy installed into the local repository.

We would like to be able to run a single command to update both
the JAR file and the WAR file. To do this, we create another
pom.xml file that invokes both projects.

&

lt

;

project

&

gt

;


     

&

lt

;

name

&

gt

;

Maven

 

2

 

Example

&

lt

;

/name&gt;


         &lt;url&gt;http:/

/

www

.

attainware

.

com

/&

lt

;

/url&gt;


     &lt;modelVersion&gt;4.0.0&lt;/

modelVersion

&

gt

;


   


     

&

lt

;

groupId

&

gt

;

com

.

attainware

.

maven2example

&

lt

;

/groupId&gt;


        &lt;version&gt;1.0&lt;/

version

&

gt

;


     

&

lt

;

artifactId

&

gt

;

maven2example_package

&

lt

;

/artifactId&gt;


     &lt;packaging&gt;pom&lt;/

packaging

&

gt

;


   


     

&

lt

;

modules

&

gt

;


       

&

lt

;

module

&

gt

;

maven2example_logic

&

lt

;

/module&gt;


       &lt;module&gt;maven2example_webapp&lt;/

module

&

gt

;


     

&

lt

;

/modules&gt;


 &lt;/

project

&

gt

;



This pom.xml file lives right above the other projects
and serves as a "master" project file.

Location of Master Project File
Figure 8. Location of master project file

Now, we can simply execute a single command, mvn install , and do a full build on both projects.

<b>

C:\maven2example&gt;mvn install


</b>


 [INFO] Scanning for   projects...


 [INFO] Reactor build order:


 [INFO]   maven2example_logic


 [INFO]   maven2example_webapp Maven Webapp


 [INFO]   Maven 2   Example


 [INFO]   -----------------------------------------------------


 [INFO] Building maven2example_logic


 [INFO]    task-segment: [install]


 [INFO]   -----------------------------------------------------


 [INFO] [resources:resources]


 [INFO] [compiler:compile]


 [INFO] [resources:testResources]


 [INFO] [compiler:testCompile]


 [INFO] [surefire:test]


 [INFO] [jar:jar]


 [INFO] [install:install]


 [INFO]   -----------------------------------------------------


 [INFO] Building maven2example_webapp Maven Webapp


 [INFO]    task-segment:   [install]


 [INFO]   -----------------------------------------------------


 [INFO] [resources:resources]


 [INFO] [compiler:compile]


 [INFO] [resources:testResources]


 [INFO] [compiler:testCompile]


 [INFO] [surefire:test]


 [INFO] [war:war]


 [INFO] [install:install]


 [INFO] [site:attach-descriptor]


 [INFO] [install:install]


 [INFO]   -----------------------------------------------------


 [INFO] Reactor Summary:


 [INFO]   -----------------------------------------------------


 [INFO] maven2example_logic   ................ SUCCESS [2.281s]


 [INFO] maven2example_webapp Maven Webapp .... SUCCESS [0.563s]


 [INFO] Maven 2 Example   .................... SUCCESS [1.156s]


 [INFO]   -----------------------------------------------------


 [INFO] BUILD SUCCESSFUL


 [INFO] -------------------------------------------------------



To summarize, we now have a project that now compiles and tests
a JAR, which is then built and installed into a WAR, which is then
in turn installed into a local web server (which is downloaded and
automatically configured). All of this with a few tiny
pom.xml files.

Repositories

You may be wondering about the use of the term
"repository." We have glossed over the use of the term,
but in brief, Maven 2 makes use of two kinds of repository: local
and remote. These repositories serve as locations for Maven 2 to
automatically pull dependencies. For example, our pom.xml
file above makes use the local repository for managing the
dependency on the JAR file, and the default Maven 2 remote
repository for managing the dependencies on JUnit and Jetty.

Generally speaking, dependencies come from either the local
repository or remote repositories. The local repository is used by
Maven 2 to store downloaded artifacts from other repositories. The
default location is based on your system. Figure 9 shows the local
repository on my laptop as of the writing of this article.

Local Repository Example
Figure 9. Local repository example

If Maven 2 can't resolve a dependency in the local repository,
it will try to resolve the dependency using a remote
repository.

The default remote repository, known as Ibiblio , includes many of the most popular open source packages. You can browse the wide range of packages on Ibiblio with the URL above and add dependencies as needed. For example, let's say that you would like to use Hibernate in your project. Navigating to www.ibiblio.org/maven/org.hibernate/poms ,
we can see there are a wide number of releases of Hibernate
available. Opening up a sample Hibernate pom file , we can see that we
simply need to add the appropriate groupId ,
artifactId , and version entries to our
business logic pom.xml file (the scope flag tells Maven 2 which
lifecycle is interested in the dependency).

&

lt

;

dependency

&

gt

;


        

&

lt

;

groupId

&

gt

;

org

.

hibernate

&

lt

;

/groupId&gt;


        &lt;artifactId&gt;hibernate&lt;/

artifactId

&

gt

;


        

&

lt

;

version

&

gt

;

3.2

.

1.ga

&

lt

;

/version&gt;


        &lt;scope&gt;compile&lt;/

scope

&

gt

;


 

&

lt

;/

dependency

&

gt

;



Simply adding this dependency to the pom.xml file will
cause Maven 2 to automatically download Hibernate and make the
appropriate JAR files available to both the business logic JAR file
and the WAR file.

You can set up your own remote repository, and add an entry to
the pom.xml file to look in that repository for artifacts.
This is extremely useful for enterprises that make use of shared
resources (for example, one group may wish to publish JAR files
that are used to access a particular piece of middleware).

Finally, you may instead wish to install your own JARs into the
local repository. For example, if you have a
Simple.jar file that someone gave you, use the command
shown below (choosing groupId and
artifactId values that are highly likely to be unique
to avoid a namespace collision).

mvn install

:

install

-

file


     

-

Dfile

=

Sample

.

jar


     

-

DgroupId

=

uniquesample


     

-

DartifactId

=

sample_jar


     

-

Dversion

=

2.1

.

3b2


     

-

Dpackaging

=

jar


     

-

DgeneratePom

=

true



Summary

In this article, we looked at how a few commands and some tiny
XML files allow us to create, compile, test, bundle, and manage
project dependencies. We built a simple web application and
deployed it to a web server with just a few commands, and we still
haven't touched on many of the features of Maven 2. For example,
additional commands generate integrated Javadocs across multiple
projects, code coverage reports, or even a complete website with
documentation. With luck, this orientation to Maven 2 has given you
enough information to begin the transition. Eventually, tools such
as Eclipse and NetBeans will almost certainly support Maven 2 (or
something like it) natively. In the meantime, you can dramatically
reduce your use of raw Ant (and spend a lot less time fighting XML
build scripts) by switching even small projects over to Maven
2.

AI 工作流和AI Agent构建指南 这些智能体能够自主感知、推理和行动,以追求特定目标,形成强大的集体智慧,可以打破孤岛,集成不同的数据源,并提供无缝的端到端自动化。如今随着AI应用的逐步深化应用和融合发展,一个新的工具应用而生,那就是工作流(Agentic Workflow智能体工作流\代理工作流)或AI agent,它们的结合正在重塑现代企业的运营模式,通过自动化、智能决策和效率优化推动数字化转型。针对这个问题,我们可以通过RAG(检索增强生成)来解决,即通过提供相关的、实时的外部数据来增强 LLM 的响应准确性**,但它仅限于。 阅读详情

相关推荐

【信息科学与工程学】【数据中心】第三十五篇 云计算数据中心的学科知识04

编号学科(课程)核心知识点在云计算/云存储/云网络/云安全/云MaaS中的作用代表教材/资料/论文 + 数学方程式列表工业界应用D1421​云原生数据库:TiDB​HTAP(混合事务/分析处理)、分布式SQL、水平扩展、强一致(Raft)、自动故障恢复、与MySQL兼容、TiFlash列式引擎提供弹性扩展的分布式数据库;支撑高并发在线交易与实时分析教材:《TiDB in Action》PingCAP(2020) 论文:《TiDB: A Raft-based HTAP Database》(2017)

weixin_49199313的博客 417

编译时的additionalrelocation overflows omitted from the output问题 [已解决]

在编译区域海洋模式ROMS的版本之一CROCO时,编译过程中(./jobcomp)出现这个报错,通过加入编译选项-fPIC解决(使用了ifort编译)。

weixin_54163482的博客 1011

SpringBoot+Vue+MySQL 高校学科竞赛平台 完整可运行版(源码+文档)

本项目基于SpringBoot+Vue+MySQL构建高校学科竞赛管理平台,覆盖学生、教师、领队教师、管理员四大角色,实现竞赛发布、报名、刷题、评分、公示等全流程闭环管理。系统架构清晰,技术主流,配套完整源码、数据库脚本、万字论文、开题报告、答辩PPT及部署文档,支持一键运行,适用于毕业设计、课程设计及院系二次开发,真正实现“开箱即用”。

招风的黑耳优快云 314

PyQt5打包生成exe文件

一、安装PyInstaller 执行命令行 pip inatall PyInstaller 二、在桌面创建一个win.py文件 import sys from PyQt5.QtWidgets import QApplication, QWidget if __name__ == '__main__': app = QApplication(sys.argv) win = QWi...

peixin_huang的博客 7052

SAC安装与相关库文件(.so)的安装

摘要 SAC: Seismic Analysis Code 是用于地震信号处理的一套软件。 本文将介绍SAC的安装,以及安装时遇到的一些问题。主要是一些动态库(lib**.so)找不到的问题。 本文的环境 64位的 Ubuntu 16.04 64位的系统是安装出现问题的主要原因 1. SAC获取与安装 SAC 的安装,只需要解压,然后修改环境变量即可!!!不需要任何编译!!! 1.1. SAC 获取与解压 SAC软件获取地址:http://ds.iris.edu/ds/nodes/dmc/forms/s.

WangCY的博客 4336

大语言模型的三种主要架构 Decoder-Only、Encoder-Only、Encoder-Decoder

f基于 Transformer 模型以非灰色显示:decoder-only 模型在蓝色分支,encoder-only 模型在粉色分支,encoder-decoder 模型在绿色分支。模型在时间线上的垂直位置表示它们的发布日期。开源模型由实心方块表示,而闭源模型由空心方块表示。右下角的堆积条形图显示了各公司和机构的模型数量。从时间轴上,我们可以看到:2021年前,当 OpenAI 决定在,他们实际上是在逆流而上。

u010899985的博客 2万+

记一次由于rootfs过大引起的内核编译错误

<br />记一次由于rootfs过大引起的内核编译错误<br /><br /> 前些日子一直在用NFS作为rootfs启动。今日想用initramfs作为rootfs制作独立运行的系统。<br /> 可是内核编译到链接阶段一直报这个错误<br /> LD      .tmp_vmlinux1<br /> arch/arm/kernel/head.o: In function `__enable_mmu_end':<br /> /home/skykingf/devlop/linux-2.6.38.

skykingf的专栏 5526

SAP OBYC自动过账全解析:从BSX到WRX的会计科目配置指南

本文深入解析SAP OBYC自动过账功能,从BSX到WRX的会计科目配置全流程,涵盖评估分组代码、事务码和评估类等核心概念。通过实战案例展示如何实现库存变动到财务过账的自动化流转,提升企业财务数据的准确性和效率。

weixin_29239279的博客 421

pythonista_什么是点子? 新Pythonista指南

pythonistaWhat is pip? pip is the standard package manager for Pyhon. It allows you to install and manage additional packages that are not part of the Python standard library. This tutorial is an intr...

cumei1658的博客 1737

LabVIEW+数据库应用系列保姆级教程,第一篇数据库连接

目录LabVIEW连接数据库的方法连接MySQL连接Oracle,SQL Server,Access等 LabVIEW连接数据库的方法 1、创建udl(Universal Data Link)是通用数据连接文件。界面操作,简单快捷。 2、编写连接字符串。需要熟悉连接字符串的结构,容易出错。 3、其他… 本教程教你使用第1种方法,也是最简单、最常用的方法。 连接MySQL 1、在MySQL Installer内安装MySQL Connector/ODBC,建议安装32位(X86),如下图: 2、创建ODBC

wwplh5520370的博客 1万+

鸿蒙开发实战:如何用DevEco Studio 4.0一键迁移安卓Java项目(附避坑清单)

本文详细介绍了如何使用DevEco Studio 4.0将安卓Java项目一键迁移到鸿蒙系统,包括环境准备、代码适配、UI转换及常见问题解决方案。通过实战指南和避坑清单,帮助开发者高效完成项目移植,充分利用鸿蒙生态优势。

weixin_29214199的博客 385

工作中使用到的单词(软件开发)_第四版

本文汇总了2020-2025年间软件开发相关的工作笔记,主要包括三版技术词汇整理(2020-2025)、优快云文章导出PDF情况,以及2025年6月的专题整理。重点内容涉及:WebSphere中jackson包加载路径问题、Java异步线程类型(守护/非守护线程)的区分与解决方案、DB2数据库连接相关指令(db2listapplications等)、AWS托管服务优势,以及OSGi框架与Java的关系说明。特别标注了技术术语的英文发音(如initiative)和敏感信息处理注意事项(敏感度标签设置)。后续

sun0322 1349

服务器搭建原神私服教程

项目的稳定版分支提供了编译好的服务端程序,但是博主觉得还是用dev版本的比较好。dev版本需要自己编译的,不要听到编译这两个字就头大,相反,这个服务端的编译相当简单。把文件中的两个IP地址127.0.0.1修改为你服务器的地址,修改完成后按 esc 输入 :wq。Mongodb作为服务端的数据库使用,我们这里安装最新版的Mongodb5。注意:如果想要使用最新版的服务端,必须安装>=17版本的java。tmux可以保证你关掉控制台的时候,程序依然继续运行。至此,服务端所需要的运行环境全部配置完成。

用最简单的方式说最重要的事!服务器、存储、云计 算、A智能的大数据产品、游戏开发技术。致力于广 大开发者、政企用户、各项机构等,构建云上世界提供全方位云计算解决方案。旨在打造差异化的开放式闭环生态系统帮助用户快速构建稳定、安全的云计算环境。 1万+

InnoDB 内存结构总览:Change Buffer、AHI 与 Log Buffer

本文系统梳理InnoDB内存结构中的ChangeBuffer、Adaptive Hash Index(AHI)与LogBuffer。ChangeBuffer缓存二级索引页不在内存时的变更,减少随机IO,仅在页加载时合并;聚簇索引修改立即执行,因数据本身不可延后。AHI基于高频等值查询自动构建哈希索引,加速定位,仅支持等值操作。LogBuffer为redo log内存缓冲,配合磁盘日志实现崩溃恢复。三者均服务于性能优化,其中ChangeBuffer与AHI共享BufferPool空间,而LogBuffer独立

bksczm的博客 307

以太网_RGMII接口与GMII接口的转化_03

RGMII接口转GMII接口!

weixin_42499230的博客 379

YOLO11改进|卷积篇|引入SPDConv

YOLO11中添加SPDConv卷积

A1983Z的博客 3698

信创环境下PageOffice文档控件适配与实践

信创适配做到文档控件这一层,拼的已经不是"有没有国产版本",而是同一个功能在两类终端上是否真的等价。等价的标准很具体:同一次点击能得到同样的结果,同一份公文能得到同样的版式,同一个项目不需要维护两套代码。这背后是三件并不轻松的工作:服务端用一套接口同时支撑两类终端,客户端把两种系统与四种芯片的安装包分发清楚,文档内核层由产品侧跟着 WPS 的版本走。浏览器这一层的压力,则被收进了产品自己的窗口里——升级节奏由产品掌握,不再交给运气。

zi_wu_xian的专栏 335

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

第三篇 第 6 课

前置:第三篇第 1 课(stdx 引入),第二篇第 1 课(std.regex)。环境:仓颉 SDK 1.2.0。本文所有代码与输出均逐行实测。

weixin_41618057的博客 557

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

【从零理解 Spring AI · 12】MCP 的两种角色:做客户端还是做服务端

上一篇讲了 MCP 的概念:标准协议、三大角色、M+N 替代 M×N。

nice_lcj520的博客 609

pcl2启动器下载(Minecraft PCL2 启动器 最新版 安装与使用说明)

摘要:PCL2(Plain Craft Launcher 2)是一款面向 Minecraft 的第三方启动器,支持正式版与快照版本管理、模组与整合包安装、Java 运行时自动匹配,绿色免安装、双击即可运行。适合在 Windows 上玩单机整合包、需要多版本共存或离线启动的用户使用

2611_96675819的博客 99

spring boot 4集成SQLLite,及其SQLLite的建库

spring boot 4集成SQLLite,及其SQLLite的建库

weixin_41463944的博客 207

Java 集合容器高频面试题全解:HashMap 树化阈值、1.7 死循环与 ConcurrentHashMap 演进(2026 招聘 JD 真题复盘)

HashMap 树化阈值 8 的泊松分布依据、退化阈值 6 的防抖设计、JDK 1.7 头插成环死循环的排查与修复、1.8 尾插后为何仍线程不安全、ConcurrentHashMap 放弃分段锁的原因与 ForwardingNode 协作扩容、computeIfAbsent 死锁陷阱、大集合找重复的场景题选型,附三层追问链自测。题目来源为 38 期公开招聘 JD 样本与字节、拼多多、建行、美团、快手公开面经。

蒙眼过河 425

spdlog 同步日志库使用详解

spdlog是一个轻量级 C++ 日志库,支持同步日志、异步日志、多线程日志、日志文件轮转和自定义日志格式。对于嵌入式 Linux 应用开发,我建议先使用。它可以帮我们完成:日志级别过滤。时间戳和线程 ID 输出。日志格式化。终端和文件输出。多线程安全写入。日志文件按大小或时间轮转。我们不需要自己实现互斥锁、文件写入、日志格式化和轮转等底层功能。

zengjiajun2006的博客 1123

JavaSE通关手册:数据类型与变量

代码无需额外处理,编译时编译器自动完成转换。

xys_678的博客 235

IDEA 简介:从零认识 IntelliJ IDEA

IDEA 全称,是由 JetBrains 公司开发的一款主流 Java 集成开发环境(IDE)。它集代码编辑、编译、调试、测试、版本控制、构建管理等功能于一体,被大量 Java 开发者视为日常开发的首选工具。所谓 IDE(Integrated Development Environment,集成开发环境),就是把写代码所需的各类工具整合到同一个界面中。相比只用文本编辑器加命令行编译,IDE 能显著减少重复操作,让开发者把精力集中在业务逻辑上。

ztI10的博客 183

Docker部署linkding:HTTPS与数据备份

部署私人书签库,除了让页面能够打开,还需要考虑几个实际问题:重建容器后数据是否保留、登录和保存是否正常、升级前怎样备份,以及出现 502 或 CSRF 错误时从哪里检查。本文使用 Docker Compose 部署 linkding,采用默认 SQLite 数据库,通过 Caddy 提供 HTTPS,并完成账户创建、数据持久化验证和备份。如果重建后账户或书签消失,应先检查是否换了项目目录、改了挂载路径,或者启动了另一套实例,不要急着重新初始化。遇到权限问题,应根据日志确认具体目录和运行用户,再调整权限。

tedcloud123的博客 191
上一篇: [- ddms] Can't bind to local 8600 for debugger
下一篇: 使用struts2中的codebehind插件
programloverjack
博客等级 码龄17年 5粉丝 252原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值