tags:

views:

99

answers:

2

Now the source code is ready, how can I build it with maven?

Suppose the source file is hw.java

I've googled some time, all the solutions requires me to set the directory in a fixed manner.

But what I want to do is keep hw.java in current directory (.), and then:

vi pom.xml
...
mkdir build
cd build
maven ...

Can I have that kind of freedom with maven?

+1  A: 

Google it!

http://cvs.peopleware.be/training/maven/maven2/helloWorld.html

http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

etc...

zaf
@Downvoter Thank you very much! Especially for your reasoning.
zaf
@zaf sometimes it's frustrating (+1)
stacker
@stacker cheers!
zaf
I did have googled before posting here!
httpinterpret
@httpinternet you have edited your question. Heavily. Next time give a thought before asking a question. It saves everyone time.
zaf
@zaf,sure,sorry for late clarification!
httpinterpret
+1  A: 

Suppose the source file is hw.java

Bad choice for a class file name in Java :)

I've googled some time,all the solutions requires me to set the directory in fixed manner.

Not really fixed but "warmly suggested" (Maven relies on conventions - the directory layout being one of them - and it is highly recommended to follow Maven's conventions if you decide to use it, less work, less troubles).

But what I want to do is keep hw.java in current directory (.) (...)

I think you'll need at least a directory below the pom.xml to store java sources.

Can I have that kind of freedom with maven?

Maven is flexible and defaults are configurable but you should really use the conventions (or another tool than Maven). I suggest to create your project using the archetype for a simple project:

mvn archetype:generate -B -DgroupId=com.stackoverflow \
                          -DartifactId=Q2841166 \
                          -Dversion=1.0-SNAPSHOT 

Then put your sources under src/main/java (in the appropriate package) and run mvn install.


Update: It looks like you don't understand what I'm saying or you just don't want to listen. In that case, feel free to modify the build element as documented in the POM reference (as I said, defaults ARE NOT MANDATORY):

<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/xsd/maven-4.0.0.xsd"&gt;
  ...
  <build>
    <sourceDirectory>${basedir}/src/main/java</sourceDirectory>
    <scriptSourceDirectory>${basedir}/src/main/scripts</scriptSourceDirectory>
    <testSourceDirectory>${basedir}/src/test/java</testSourceDirectory>
    <outputDirectory>${basedir}/target/classes</outputDirectory>
    <testOutputDirectory>${basedir}/target/test-classes</testOutputDirectory>
    ...
  </build>
</project>

Now, because changing these defaults is not recommended, because Maven doesn't encourage doing this, because it's a bad idea if you're not dealing with a legacy project, because it's useless work if you don't have to, because this will cause more troubles than benefits, don't expect to find a "tutorial" about this. Let me rephrase it: what you're asking for is a "how to shoot myself in the foot" tutorial and that's just ridiculous.

Last thing: if Maven doesn't work the way you want it to work, don't use Maven, that's the best advice I can give you.

Pascal Thivent
This is not default,but mandatory.
httpinterpret
@httpinterpret Nothing is mandatory, defaults **can** be changed. But I do not recommend to go this way.
Pascal Thivent
There is no tutorial on how one **can** change the defaults at all.
httpinterpret
This is because the Maven community strongly discourages changing said defaults, @httpinterpret.
JUST MY correct OPINION
Then they should document it as **mandatory**,not **defaults**.
httpinterpret
@httpinterpret No they should **not** and the way to change **is** documented. There is just no tutorial about the wrong way to do things (I wonder why...).
Pascal Thivent
Finally I gave up this trying...
httpinterpret