where to put mybatis.xml - eclipse wants it in src, maven wants it in resources.

1,399 views
Skip to first unread message

Simon

unread,
Aug 22, 2010, 7:04:28 AM8/22/10
to mybatis-user
If I put mybatis.xml (with the db settings etc), into src/main/java/
com/mydomain/myproject/mybatis.xml, anything I run in eclipse (e.g.
testNG suites, any main(), the webapp deployed to the internal eclipse
tomcat etc. works fine.

However, if i do a mvn install by command line, it doesnt package up
the xml files, as the war wont run on an external tomcat.

If I move the mybatis into source/main/resources/com/mydomain/
myproject/mybatis, then external mvn works, and puts it in the jar,
but now eclipse cant find it.

I could duplicate the file in both places, but his would lead to one
always being fogotten and out fo date.

The code accesses the xml file thusly:

private final static String resource = "com/skillkash/ge/core/
mybatis.xml";

public static synchronized SqlSessionFactory getFactory() {
reader = Resources.getResourceAsReader(resource);
:
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
:
return sqlSessionFactory;
}

I guess ill have the same problem with the mapper.xml files.

Does anyone got a good solution?

Simon

unread,
Aug 22, 2010, 7:10:27 AM8/22/10
to mybatis-user
As a temp fix I manually added it to the eclipse source java build
path. However, I would guess this be overwritten if I ever need to do
a:
mvn eclipse:eclipse

What is the best practice?

Thanks,

Simone Tripodi

unread,
Aug 22, 2010, 7:26:59 AM8/22/10
to mybati...@googlegroups.com
Hi Simon,
I suggest you to follow the Maven "pattern" then run in the shell
maven eclipse:eclipse, then refresh the eclipse project. Otherwise you
can install the Sonatype m2eclipse[1], I've been working with it for
more than one year and I find it very comfortable.
Good luck,
Simo

[1] http://m2eclipse.sonatype.org/

http://people.apache.org/~simonetripodi/
http://www.99soft.org/

Clinton Begin

unread,
Aug 22, 2010, 9:24:58 AM8/22/10
to mybati...@googlegroups.com
This is one maven convention I choose to break, be it mybatis or
hibernate... I add the /src as a resources entry in the pom so that I
can keep my mapper xml alongside the java interface, and don't have to
duplicate the entire java package tree.

But as simone said, it should work the normal way too. I use intellij
,d just open the pom directly. And it figures everything out.

Clinton

--
Sent from my mobile device

Jeff Butler

unread,
Aug 22, 2010, 10:36:13 AM8/22/10
to mybati...@googlegroups.com
Add the resources directory as a second source directory to the
project. In many of my Maven projects in Eclipse I have four source
directories: src/main/java, src/main/resources, src/test/java,
src/test/resources. When I went with Maven I made a conscious
decision to follow Maven standards as closely as possible. Eclipse
has no problems with multiple source paths.

Jeff Butler

Guy Rouillier

unread,
Aug 22, 2010, 11:20:03 PM8/22/10
to mybati...@googlegroups.com
On 8/22/2010 9:24 AM, Clinton Begin wrote:
> This is one maven convention I choose to break, be it mybatis or
> hibernate... I add the /src as a resources entry in the pom so that I
> can keep my mapper xml alongside the java interface, and don't have to
> duplicate the entire java package tree.

I don't know how that package tree thing got started with the MyBatis
resource files, but it isn't necessary. I have my projects arranged
like this:

src/com/mycompany/myproject/db/MyMapper.java

resources/
MyBatisConfig.xml (datasource, type handlers, etc)
MyMapper.xml (mapped statements)
ibatorConfig.xml (Ibator configuration)

MyMapper.xml has this:
<mapper namespace="com.mycompany.myproject.db.MyMapper">

This all works fine, and dispenses with unnecessary layers of directory
entries. I add "resources" as a source directory in Eclipse.

--
Guy Rouillier

Simon

unread,
Aug 23, 2010, 6:40:34 AM8/23/10
to mybatis-user
Hi, thanks for the great tips. I am using m2eclipse, and created the
projects only using its gui based maven prototype wizard. However,
most of these dont even create the correct directory structure, and
they dont automatically add the resources dirs to the eclipse Java
source paths. At least I now know adding the eclipse source path by
hand is common practice, and that it wasnt something I was doing wrong
with m2eclipse/maven.

On Aug 22, 1:26 pm, Simone Tripodi <simone.trip...@gmail.com> wrote:
> Hi Simon,
> I suggest you to follow the Maven "pattern" then run in the shell
> maven eclipse:eclipse, then refresh the eclipse project. Otherwise you
> can install the Sonatype m2eclipse[1], I've been working with it for
> more than one year and I find it very comfortable.
> Good luck,
> Simo
>
> [1]http://m2eclipse.sonatype.org/
>
> http://people.apache.org/~simonetripodi/http://www.99soft.org/
Reply all
Reply to author
Forward
0 new messages